String indexOf Performance

Aleksey Shipilev shade at redhat.com
Fri Jan 27 20:19:01 UTC 2017


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



More information about the jdk9-dev mailing list