comparision of classpath and gcj java.io.StreamTokenizer
Per Bothner
per@bothner.com
Wed Jul 26 01:04:00 GMT 2000
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/
More information about the Java
mailing list