Created
February 9, 2012 18:56
-
-
Save tsuna/1782008 to your computer and use it in GitHub Desktop.
Java integer hash set micro-benchmark
This file contains hidden or 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
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
import com.stumbleupon.mr.HashIntSet; // OpenJDK's HashSet hacked to use built-in `int' | |
import gnu.trove.set.hash.TIntHashSet; | |
import com.google.caliper.Param; | |
import com.google.caliper.SimpleBenchmark; | |
final class setbench { | |
public static class HashSetBench extends SimpleBenchmark { | |
private HashSet<Integer> hs; | |
@Param int size; | |
@Override protected void setUp() { | |
hs = new HashSet<Integer>(size); | |
for (int i = 0; i < size; i++) { | |
hs.add(i); | |
} | |
} | |
public int timeContains(final int n) { | |
int dummy = 0; | |
for (int i = 0; i < n; i++) { | |
if (hs.contains(i)) { | |
dummy++; | |
} | |
} | |
return dummy; | |
} | |
} | |
public static class HashIntSetBench extends SimpleBenchmark { | |
private HashIntSet hs; | |
@Param int size; | |
@Override protected void setUp() { | |
hs = new HashIntSet(size); | |
for (int i = 0; i < size; i++) { | |
hs.add(i); | |
} | |
} | |
public int timeContains(final int n) { | |
int dummy = 0; | |
for (int i = 0; i < n; i++) { | |
if (hs.contains(i)) { | |
dummy++; | |
} | |
} | |
return dummy; | |
} | |
} | |
public static class TIntHashSetBench extends SimpleBenchmark { | |
private TIntHashSet hs; | |
@Param int size; | |
@Override protected void setUp() { | |
hs = new TIntHashSet(size); | |
for (int i = 0; i < size; i++) { | |
hs.add(i); | |
} | |
} | |
public int timeContains(final int n) { | |
int dummy = 0; | |
for (int i = 0; i < n; i++) { | |
if (hs.contains(i)) { | |
dummy++; | |
} | |
} | |
return dummy; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
contains() speed
HashSet<Integer>
HashIntSet
TIntHashSet
Memory footprint
HashMap$Entry
+ 16 bytes for Integer)HashIntMap$Entry
int
in an array and onebyte
in another array).