I've encountered a strange problem in production today. Though I love Guava, I ran into a use case where Guava's Sets.intersection()
was performing pretty badly. I've written a sample code:
Set<Long> cache = new HashSet<>();
for (long i = 0; i < 1000000; i++) {
cache.add(i);
}
Set<Long> keys = new HashSet<>();
for (long i = 0; i < 100; i++) {
keys.add(i);
}
long start = System.currentTimeMillis();
Set<Long> foundKeys = new HashSet<>();
for (Long key : keys) {
if (cache.contains(key)) {
foundKeys.add(key);
}
}
System.out.println("Java search: " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
SetView<Long> intersection = Sets.intersection(keys, cache);
System.out.println("Guava search: " + (System.currentTimeMillis() - start));
I've tried to create a similar production scenario where I've a keys cache, and I'm looking for all the keys present in the cache. Strangely, Guava search is taking a lot longer than Java search. After running this I got:
Java search: 0
Guava search: 36
Can anyone tell why this isn't suited for my use case or is there a bug in Guava?