Tom Copeland's Recent Posts

RSS Feeds

« Even faster StringBuffer.append() | Main | Faster, simpler return statements »

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451d3c069e200e55070ab048834

Listed below are links to weblogs that reference Fast String.indexOf():

Comments

now you have one for
sb.append('c')
and
str.indexOf('c')

and possible more with a single character.


Wouldn't it be possible to make a general rule for all places where you use a single string character and make it to a char?
I think its always better to that.. or did i overlook some cases where it's not possible?

nevermind.. it ofcourse have to accept a char.
but what about concatenation with a single letter or is there a rule for that already?

str+'c'

Interesting rule Tom!

Is there the same performance benefit (and if so, does your new rule catch) this:

int iSearchPos = 5;
String string = ....;
string.indexOf('c', iSearchPos);

(searching from a given position)

Hi Allan -
Yup, it catches that case... although I hadn't thought about it when coding that rule up, thanks for the pointer! I've added a test case for it...
Yours,
Tom

Hi simonsays -
Hm, I suppose we could combine these two rules... hm. I'm not sure. I kind of feel like it might be trying to do too much, since AppendCharacterWithChar deals with StringBuffers and UseIndexOfChar deals with strings.
Yours,
Tom

Looking at the code for String.indexOf(String) may be misleading. A good JVM will treat the method as an intrinsic.

Incidentally, String.concat may be faster than using the String + operator. Particularly from 1.5.

Tom -
Hm, good point. I've been using JDK 1.4 for benchmarking... perhaps I should give it a whirl on something newer.
Great stuff on your blog, by the way; thanks for posting those. I like the builder suggestions here especially:
http://jroller.com/page/tackline?entry=static_creation_methods
Tom

The comments to this entry are closed.