Streamtokenizer

Andrew Haley aph@redhat.com
Fri May 9 10:51:00 GMT 2008


OneGuy wrote:
> I was reading
> 
> http://www.docjar.com/html/api/java/io/StreamTokenizer.java.html
> 
> and it sets nval (i.e number token) with this method.
> 
> nval = Double.valueOf(tokbuf.toString()).doubleValue();
> 
> This basically creates a String Object from charbuf I guess and parses
> the String to number.
> 
> Sun's version doesn't create String object and parses the number this way...
> 
> 
> 		else if ('0' <= c && c <= '9') {
> 		    v = v * 10 + (c - '0');
> 		    decexp += seendot;
> 		} else
> 		    break;
> 		c = read();
> 	    }
> 	    peekc = c;
> 	    if (decexp != 0) {
> 		double denom = 10;
> 		decexp--;
> 		while (decexp > 0) {
> 		    denom *= 10;
> 		    decexp--;
> 		}
> 		/* Do one division of a likely-to-be-more-accurate number */
> 		v = v / denom;
> 	    }
> 	    nval = neg ? -v : v;
> 	    return ttype = TT_NUMBER;
> 	}
> 
> Perhaps this was the reason why it was 10 times slower in this
> benchmark since all sumcol benchmark does is parse huge number of
> numbers from Stream and adds the number.

Could be.  That's one of the problems of microbenchmarks: they do
unrealistic things and tend to magnify the importance of a tiny part
of the code base.  The famous Dhrystone benchmark was hyper-sensitive
to the performance of string copying.

Andrew.



More information about the Java mailing list