390fa95f32
Motivation: `instanceOf` doesn't perform null check like `getClass()` does. So `instanceOf` may be faster. However, it not true for all cases, as C2 could eliminate these null checks for `getClass()`. Modification: Replaced `string.getClass() == AsciiString.class` with `string instanceof AsciiString`. Proof: ``` @BenchmarkMode(Mode.Throughput) @Fork(value = 1) @State(Scope.Thread) @Warmup(iterations = 5, time = 1, batchSize = 1000) @Measurement(iterations = 10, time = 1, batchSize = 1000) public class GetClassInstanceOf { Object key; @Setup public void setup() { key = "123"; } @Benchmark public boolean getClassEquals() { return key.getClass() == String.class; } @Benchmark public boolean instanceOf() { return key instanceof String; } } ``` ``` Benchmark Mode Cnt Score Error Units GetClassInstanceOf.getClassEquals thrpt 10 401863.130 ± 3092.568 ops/s GetClassInstanceOf.instanceOf thrpt 10 421386.176 ± 4317.328 ops/s ``` |
||
---|---|---|
.. | ||
src | ||
pom.xml |