String indexOf Performance

Scott Palmer swpalmer at gmail.com
Fri Jan 27 20:21:56 UTC 2017


On Fri, Jan 27, 2017 at 3:19 PM, Aleksey Shipilev <shade at redhat.com> wrote:
> On 01/27/2017 09:14 PM, Scott Palmer wrote:
>> JMH results:
>>
>> Benchmark                              Mode  Cnt      Score     Error  Units
>> IndexOfBenchmark.StringIndexOfChar    thrpt  200  45387.001 ± 175.235  ops/s
>> IndexOfBenchmark.StringIndexOfString  thrpt  200  81835.195 ± 157.637  ops/s
>>
>>
>> My test methods are simple:
>>
>>     static final String [] paths = new String[1000];
>>     static {
>>         for (int i = 0; i < paths.length; i++) {
>>             paths[i] = ((i & 1) == 0) ? randomName() : randomNameWithSlash();
>>         }
>>     }
>>
>>   @Benchmark
>>     public static int StringIndexOfChar() {
>>         int acc = 0;
>>         for (String s : paths) {
>>             if (s.indexOf('/') > -1) {
>>                 acc++;
>>             }
>>         }
>>         return acc;
>>     }
>>
>>     @Benchmark
>>     public static int StringIndexOfString() {
>>         int acc = 0;
>>         for (String s : paths) {
>>             if (s.indexOf("/") > -1) {
>>                 acc++;
>>             }
>>         }
>>         return acc;
>>     }
>
> Please post the full test somewhere. The randomName() and randomNameWithSlash()
> are important (missing) parts of it.
>
> -Aleksey

Sorry (they were the same as in the previous code I linked).  Here is
the full source:

http://pastebin.com/zRXKwD64

Regards,

Scott


More information about the jdk9-dev mailing list