Optimize string operations

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Optimize string operations

Otávio Gonçalves de Santana
This PR has the goal to optimize String operations in the module container.

   - The first operation is instead of using the method isEmpty instead of
   use string.equals("")
   - The second operation changes a String with one characater to a char
   type.

@Warmup(iterations = 5, time = 1)@Measurement(iterations = 20, time =
1)@Fork(3)@BenchmarkMode(Mode.Throughput)@OutputTimeUnit(TimeUnit.MILLISECONDS)@State(Scope.Thread)public
class StringBenchmark {

    private static final int COUNT = 300;
    private static final String TEXT = RandomStringUtils.randomAscii(COUNT);

    @Benchmark
    public boolean equals() {
        return TEXT.equals("");
    }

    @Benchmark
    public boolean isEmpty() {
        return TEXT.isEmpty();
    }

    @Benchmark
    public int indexOfString() {
        return TEXT.indexOf("a");
    }

    @Benchmark
    public int indexOfChar() {
        return TEXT.indexOf('a');
    }

}

Result:
Benchmark Mode Cnt Score unit
equals thrpt 60 333414,466 ops/ms
isEmpty thrpt 60 380694,204 (around 20%faster) ops/ms
indexOfString thrpt 60 78423,569 ops/ms
indexOfChar thrpt 60 105625,484(around 20%faster) ops/ms

Ref: https://github.com/apache/tomee/pull/261