String indexOf Performance

Aleksey Shipilev shade at redhat.com
Fri Jan 27 17:47:27 UTC 2017


On 01/27/2017 06:37 PM, Andrew Haley wrote:
> On 27/01/17 17:20, Scott Palmer wrote:
>> I’m not sure if this is the right list, please redirect me if it is not.
>>
>> I was looking into optimizing some code and was surprised to find that String.indexOf(char) was significantly slower than String.indexOf(String) for a single character search.  (E.g. I’m looking for a slash / in a path.)
>> The seems counter intuitive, but on Java 8 I saw String.indexOf(char) was about 30% slower than String.indexOf(String).  Then I tested on Java 9 and found the performance difference was actually greater because indesOf(String) was now faster, but indexOf(char) was about the same speed.  I presume this is related to the new compact strings.
>>
>> Am I missing something or should I not expect indexOf(char) to be as fast or faster than indexOf(String) for a string of length 1?
> 
> What hardware?  The hardware matters because intrinsics use SSE 4.2.
> 
> And how long were the strings?

Better yet, just show your benchmark :)

-Aleksey



More information about the jdk9-dev mailing list