This is the mail archive of the
java-discuss@sources.redhat.com
mailing list for the Java project.
Re: comparision of classpath and gcj java.io.StreamTokenizer
- To: Oskar Liljeblad <osk at hem dot passagen dot se>
- Subject: Re: comparision of classpath and gcj java.io.StreamTokenizer
- From: Per Bothner <per at bothner dot com>
- Date: 26 Jul 2000 01:09:00 -0700
- Cc: java-discuss at sourceware dot cygnus dot com
- References: <20000723015316.A7193@oskar>
Oskar Liljeblad <osk@hem.passagen.se> writes:
> nextToken gcj is better: gcj uses StringBuffer for token char store,
> cp uses char array of BUFFER_INCREMENT_SIZE.
Personally, I prefer using two fields: a char[] and an int length.
That's more efficient than a StringBuffer. However, using a fixed-size
BUFFER_INCREMENT_SIZE is a no-no, as that leads to quadratic behavior.
Instead, double the length. That makes adding a char a constant-time
operation on average:
if (tokbuf.length == toklen)
{
char[] tmp = new char[2 * toklen];
System.arraycopy(tokbuf, 0, tmp, 0, toklen);
tokbuf = tmp;
}
tokbuf[toklen++] = ...;
I use this idiom all the time.
--
--Per Bothner
per@bothner.com http://www.bothner.com/~per/