I think String.indexOf(char)
is a little more faster than
String.indexOf(String)
when using single character & single String(ex, \'x\' & &q
Your JMH test is incorrect as you don't consume the result, so the indexOf
call can be (or can be not) removed at all by JIT compiler. In your case it seems that JIT-compiler determined that indexOf(String)
has no side-effect and removed this call at all, but did not do the same for indexOf(char)
. Always consume the result (the simplest way is to return it from the benchmark). Here's my version:
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
public class IndexOfTest {
private String str;
private char c;
private String s;
@Setup
public void setup() {
str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
c = 'z';
s = "z";
}
@Benchmark
public int indexOfChar() {
return str.indexOf('z');
}
@Benchmark
public int indexOfString() {
return str.indexOf("z");
}
@Benchmark
public int indexOfCharIndirect() {
return str.indexOf(c);
}
@Benchmark
public int indexOfStringIndirect() {
return str.indexOf(s);
}
}
I test the same thing, but added two indirect tests: when searched char or String is loaded from the field, thus its exact value is unknown during the JIT-compilation. The results are the following (Intel x64):
# JMH 1.11.2 (released 27 days ago)
# VM version: JDK 1.8.0_45, VM 25.45-b02
Benchmark Mode Cnt Score Error Units
IndexOfTest.indexOfChar avgt 30 25,364 ± 0,424 ns/op
IndexOfTest.indexOfCharIndirect avgt 30 25,287 ± 0,210 ns/op
IndexOfTest.indexOfString avgt 30 24,370 ± 0,100 ns/op
IndexOfTest.indexOfStringIndirect avgt 30 27,198 ± 0,048 ns/op
As you can see, indexOfChar
performs in the same way regardless of direct or indirect access. The indexOfString
is slightly faster for direct access, but somewhat slower for indirect. That's because indexOf(String)
is a JVM intrinsic: its Java code is actually replaced by JIT compiler with efficient inline implementation. For constant string known at JIT compilation time it's possible to generate more efficient code.
In general there's no big difference at least for such short strings. Thus you may use either of these methods for single symbol match.