Skip to content

Instantly share code, notes, and snippets.

@qwwdfsad
Last active May 10, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwwdfsad/01981441bc48cea5bdef0c9a5daf404b to your computer and use it in GitHub Desktop.
Save qwwdfsad/01981441bc48cea5bdef0c9a5daf404b to your computer and use it in GitHub Desktop.
package ru.qwwdfsad.benchmarks;
import org.apache.lucene.util.OpenBitSet;
import org.openjdk.jmh.annotations.*;
import java.util.BitSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 2, jvmArgs = {"-ea"})
@Warmup(iterations = 5)
@Measurement(iterations = 5)
public class BitSetFillFactorBenchmark {
private static final ThreadLocalRandom random = ThreadLocalRandom.current();
@State(Scope.Benchmark)
public static class BitSetState {
@Param({"1232896"})
private int setSize;
@Param({"0.01", "0.1", "0.5", "1"})
private double fillFactor;
BitSet bitSet;
OpenBitSet openBitSet;
// To benchmark or/and
BitSet otherBitSet;
OpenBitSet otherOpenBitSet;
@Setup(Level.Iteration)
public void setup() {
bitSet = new BitSet(setSize);
openBitSet = new OpenBitSet(setSize);
otherBitSet = new BitSet(setSize);
otherOpenBitSet = new OpenBitSet(setSize);
// Fill only first fillFactor% of set to show BitSet optimization
int part = (int) (setSize * fillFactor);
for (int i = 0; i < part / 2; i++) {
int nextBit = random.nextInt(part);
bitSet.set(nextBit);
openBitSet.set(nextBit);
nextBit = random.nextInt(part);
otherBitSet.set(nextBit);
otherOpenBitSet.set(nextBit);
}
}
}
/*
* For following benchmarks state is preserved between
* invocations, but that shouldn't make any noise in results
* as long as implementation doesn't check if it should
* or/and bits
*/
@Benchmark
public void andClassic(BitSetState state) {
state.bitSet.and(state.otherBitSet);
}
@Benchmark
public void andOpen(BitSetState state) {
state.openBitSet.and(state.otherOpenBitSet);
}
@Benchmark
public void orClassic(BitSetState state) {
state.bitSet.or(state.otherBitSet);
}
@Benchmark
public void orOpen(BitSetState state) {
state.openBitSet.or(state.otherOpenBitSet);
}
}
Benchmark (fillFactor) (setSize) Mode Cnt Score Error Units
BitSetBenchmark.andClassic 0.01 1232896 thrpt 5 32.036 ± 1.320 ops/us
BitSetBenchmark.andClassic 0.1 1232896 thrpt 5 3.824 ± 0.896 ops/us
BitSetBenchmark.andClassic 0.5 1232896 thrpt 5 0.330 ± 0.027 ops/us
BitSetBenchmark.andClassic 1 1232896 thrpt 5 0.140 ± 0.017 ops/us
BitSetBenchmark.andOpen 0.01 1232896 thrpt 5 0.142 ± 0.008 ops/us
BitSetBenchmark.andOpen 0.1 1232896 thrpt 5 0.128 ± 0.015 ops/us
BitSetBenchmark.andOpen 0.5 1232896 thrpt 5 0.112 ± 0.015 ops/us
BitSetBenchmark.andOpen 1 1232896 thrpt 5 0.132 ± 0.018 ops/us
BitSetBenchmark.orClassic 0.01 1232896 thrpt 5 27.826 ± 13.312 ops/us
BitSetBenchmark.orClassic 0.1 1232896 thrpt 5 3.727 ± 1.161 ops/us
BitSetBenchmark.orClassic 0.5 1232896 thrpt 5 0.342 ± 0.022 ops/us
BitSetBenchmark.orClassic 1 1232896 thrpt 5 0.133 ± 0.021 ops/us
BitSetBenchmark.orOpen 0.01 1232896 thrpt 5 0.133 ± 0.009 ops/us
BitSetBenchmark.orOpen 0.1 1232896 thrpt 5 0.118 ± 0.007 ops/us
BitSetBenchmark.orOpen 0.5 1232896 thrpt 5 0.127 ± 0.018 ops/us
BitSetBenchmark.orOpen 1 1232896 thrpt 5 0.148 ± 0.023 ops/us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment