f4b6a3 / tsid-creator

A Java library for generating Time-Sorted Unique Identifiers (TSID).
MIT License
475 stars 50 forks source link

Optimize comparison and hash #16

Closed fabiolimace closed 2 years ago

fabiolimace commented 2 years ago

Benchmark results before the changes:

Benchmark              Mode  Cnt       Score      Error   Units
Throughput.compareTo  thrpt    5  220079,691 ±  517,983  ops/ms
Throughput.equals     thrpt    5  324611,500 ± 3358,493  ops/ms
Throughput.hashCode   thrpt    5  419975,993 ± 4158,182  ops/ms

Benchmark results after the changes:

Benchmark              Mode  Cnt       Score       Error   Units
Throughput.compareTo  thrpt    5  354694,538 ± 10091,845  ops/ms (1.611667)
Throughput.equals     thrpt    5  437656,101 ±  2504,245  ops/ms (1.348248)
Throughput.hashCode   thrpt    5  459955,540 ±  4697,826  ops/ms (1.095196)

Benchmark code:


package benchmark;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;

import com.github.f4b6a3.tsid.Tsid;
import com.github.f4b6a3.tsid.TsidCreator;

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

    Tsid tsid1 = new Tsid(0);
    Tsid tsid2 = new Tsid(0);

    @Benchmark
    public int compareTo() {
        return tsid1.compareTo(tsid2);
    }

    @Benchmark
    public boolean equals() {
        return tsid1.equals(tsid2);
    }

    @Benchmark
    public int hashCode() {
        return tsid1.hashCode();
    }
}
fabiolimace commented 2 years ago

Released v5.0.1