Skip to content

Instantly share code, notes, and snippets.

@zakki
Created June 9, 2016 07:34
Show Gist options
  • Save zakki/66d785b4a9d3c64bc9f8199d7e290e07 to your computer and use it in GitHub Desktop.
Save zakki/66d785b4a9d3c64bc9f8199d7e290e07 to your computer and use it in GitHub Desktop.
import java.util.Random;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.impl.factory.primitive.IntIntMaps;
public class Main {
public static void time(String name, Runnable f) {
long s = System.nanoTime();
f.run();
System.out.println(String.format("%s: %d elapsed.", name, (long)(((System.nanoTime() - s) / 1e6))));
}
public static void main(String[] args) {
int[] source = new int[50000000];
Random r = new Random();
for (int i =0; i < source.length; i++)
source[i] = r.nextInt();
// Map<Integer, Integer> m = new HashMap<>();
// MutableMap<Integer, Integer> m = Maps.mutable.empty();
MutableIntIntMap m = IntIntMaps.mutable.empty();
time("Insertion", () -> {
for (int x : source) {
m.put(x, 0);
}
});
time("Lookups", () -> {
int acc = 0;
for (int x : source) {
acc += m.get(x) % 10;
}
System.out.println(acc);
});
}
}
// Insertion: 3399 elapsed.
// Lookups: 1494 elapsed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment