Last active
May 10, 2016 10:00
-
-
Save qwwdfsad/5cca78e6c59f49952695a8eb277c5fc2 to your computer and use it in GitHub Desktop.
BitSet vs OpenBitSet performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 1, jvmArgs = {"-ea"}) | |
@Warmup(iterations = 5) | |
@Measurement(iterations = 5) | |
public class BitSetBenchmark { | |
private static final ThreadLocalRandom random = ThreadLocalRandom.current(); | |
@State(Scope.Benchmark) | |
public static class BitSetState { | |
@Param({"1024", "1232896"}) | |
private int setSize; | |
BitSet bitSet; | |
OpenBitSet openBitSet; | |
// To benchmark or/and | |
BitSet otherBitSet; | |
OpenBitSet otherOpenBitSet; | |
int nextIndex; | |
@Setup(Level.Iteration) | |
public void setup() { | |
bitSet = new BitSet(setSize); | |
openBitSet = new OpenBitSet(setSize); | |
otherBitSet = new BitSet(setSize); | |
otherOpenBitSet = new OpenBitSet(setSize); | |
for (int i = 0; i < setSize / 2; i++) { | |
int nextBit = random.nextInt(setSize); | |
bitSet.set(nextBit); | |
openBitSet.set(nextBit); | |
nextBit = random.nextInt(setSize); | |
otherBitSet.set(nextBit); | |
otherOpenBitSet.set(nextBit); | |
} | |
nextIndex = random.nextInt(setSize); | |
} | |
} | |
@Benchmark | |
public int cardinalityClassic(BitSetState state) { | |
return state.bitSet.cardinality(); | |
} | |
@Benchmark | |
public long cardinalityOpen(BitSetState state) { | |
return state.openBitSet.cardinality(); | |
} | |
@Benchmark | |
public boolean getClassic(BitSetState state) { | |
return state.bitSet.get(state.nextIndex); | |
} | |
@Benchmark | |
public boolean getOpen(BitSetState state) { | |
return state.openBitSet.get(state.nextIndex); | |
} | |
@Benchmark | |
public boolean getOpenFast(BitSetState state) { | |
return state.openBitSet.fastGet(state.nextIndex); | |
} | |
@Benchmark | |
public void setClassic(BitSetState state) { | |
state.bitSet.set(state.nextIndex); | |
} | |
@Benchmark | |
public void setOpen(BitSetState state) { | |
state.openBitSet.set(state.nextIndex); | |
} | |
@Benchmark | |
public void setOpenFast(BitSetState state) { | |
state.openBitSet.fastSet(state.nextIndex); | |
} | |
@Benchmark | |
public void setNextClassic(BitSetState state) { | |
state.bitSet.nextSetBit(state.nextIndex); | |
} | |
@Benchmark | |
public void setNextOpen(BitSetState state) { | |
state.openBitSet.nextSetBit(state.nextIndex); | |
} | |
/* | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Benchmark (setSize) Mode Cnt Score Error Units | |
BitSetBenchmark.andClassic 1024 thrpt 10 138.027 ± 6.181 ops/us | |
BitSetBenchmark.andOpen 1024 thrpt 10 115.974 ± 3.643 ops/us | |
BitSetBenchmark.cardinalityClassic 1024 thrpt 10 127.056 ± 8.407 ops/us | |
BitSetBenchmark.cardinalityOpen 1024 thrpt 10 29.440 ± 0.997 ops/us | |
BitSetBenchmark.getClassic 1024 thrpt 10 263.362 ± 37.809 ops/us | |
BitSetBenchmark.getOpen 1024 thrpt 10 235.322 ± 19.588 ops/us | |
BitSetBenchmark.getOpenFast 1024 thrpt 10 268.796 ± 42.088 ops/us | |
BitSetBenchmark.orClassic 1024 thrpt 10 140.262 ± 3.626 ops/us | |
BitSetBenchmark.orOpen 1024 thrpt 10 106.204 ± 3.699 ops/us | |
BitSetBenchmark.setClassic 1024 thrpt 10 516.714 ± 24.960 ops/us | |
BitSetBenchmark.setOpen 1024 thrpt 10 502.633 ± 33.051 ops/us | |
BitSetBenchmark.setOpenFast 1024 thrpt 10 552.434 ± 13.342 ops/us | |
BitSetBenchmark.setNextClassic 1024 thrpt 10 589.595 ± 36.034 ops/us | |
BitSetBenchmark.setNextOpen 1024 thrpt 10 508.038 ± 25.773 ops/us | |
Benchmark (setSize) Mode Cnt Score Error Units | |
BitSetBenchmark.andClassic 1232896 thrpt 10 124.341 ± 23.352 ops/us | |
BitSetBenchmark.andOpen 1232896 thrpt 10 0.129 ± 0.006 ops/us | |
BitSetBenchmark.cardinalityClassic 1232896 thrpt 10 130.825 ± 4.589 ops/us | |
BitSetBenchmark.cardinalityOpen 1232896 thrpt 10 0.048 ± 0.002 ops/us | |
BitSetBenchmark.getClassic 1232896 thrpt 10 269.467 ± 11.020 ops/us | |
BitSetBenchmark.getOpen 1232896 thrpt 10 286.761 ± 5.410 ops/us | |
BitSetBenchmark.getOpenFast 1232896 thrpt 10 290.271 ± 5.234 ops/us | |
BitSetBenchmark.orClassic 1232896 thrpt 10 143.471 ± 1.735 ops/us | |
BitSetBenchmark.orOpen 1232896 thrpt 10 0.122 ± 0.013 ops/us | |
BitSetBenchmark.setClassic 1232896 thrpt 10 531.660 ± 23.515 ops/us | |
BitSetBenchmark.setOpen 1232896 thrpt 10 534.806 ± 9.774 ops/us | |
BitSetBenchmark.setOpenFast 1232896 thrpt 10 532.678 ± 38.644 ops/us | |
BitSetBenchmark.setNextClassic 1232896 thrpt 10 589.091 ± 22.262 ops/us | |
BitSetBenchmark.setNextOpen 1232896 thrpt 10 498.391 ± 12.890 ops/us |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Benchmark (setSize) Mode Cnt Score Error Units | |
BitSetBenchmark.getClassic 1024 thrpt 5 0.231 ± 0.066 ops/ns | |
BitSetBenchmark.getClassic 1232896 thrpt 5 0.225 ± 0.079 ops/ns | |
BitSetBenchmark.getOpen 1024 thrpt 5 0.244 ± 0.061 ops/ns | |
BitSetBenchmark.getOpen 1232896 thrpt 5 0.242 ± 0.062 ops/ns | |
BitSetBenchmark.getOpenFast 1024 thrpt 5 0.255 ± 0.066 ops/ns | |
BitSetBenchmark.getOpenFast 1232896 thrpt 5 0.244 ± 0.078 ops/ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment