Skip to content

Instantly share code, notes, and snippets.

@llogiq
Created July 28, 2014 13:03
Show Gist options
  • Save llogiq/27bed898dcf36fc47544 to your computer and use it in GitHub Desktop.
Save llogiq/27bed898dcf36fc47544 to your computer and use it in GitHub Desktop.
Caliper Benchmark pitting trove's TIntHashSet against hftc's HashIntSet (mixed add and contains)
import gnu.trove.set.TIntSet;
import gnu.trove.set.hash.TIntHashSet;
import java.util.concurrent.ThreadLocalRandom;
import net.openhft.collect.set.hash.HashIntSet;
import com.google.caliper.Benchmark;
import com.google.caliper.runner.CaliperMain;
public class IntHashSetBenchmark extends Benchmark {
static final int SIZE = 100000;
int[] values = new int[SIZE];
@Override
protected void setUp() throws Exception {
final ThreadLocalRandom r = ThreadLocalRandom.current();
for(int i = 0; i < SIZE; i++) {
values[i] = r.nextInt(SIZE << 1);
}
}
public int timeHftcHash(final int reps) {
int result = 0;
for(int r = 0; r < reps; r++) {
final HashIntSet hftc = net.openhft.collect.set.hash.HashIntSets.newMutableSet();
for(int i = 0; i < SIZE; i += 2) {
hftc.add(values[i]);
}
for(final int v : values) {
result += hftc.contains(v) ? v : 1;
}
}
return result;
}
public int timeTroveHash(final int reps) {
int result = 0;
for(int r = 0; r < reps; r++) {
final TIntSet trove = new TIntHashSet();
for(int i = 0; i < SIZE; i += 2) {
trove.add(values[i]);
}
for(final int v : values) {
result += trove.contains(v) ? v : 1;
}
}
return result;
}
public static void main(final String[] args) {
CaliperMain.main(IntHashSetBenchmark.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment