Friday, March 28, 2025

Lookup Speed of uint Sets in Go

[]uint vs map[uint]struct{}

A wee bit of premature optimization, I was coding a thing where I wanted a set of uint, and I wanted to filter several million records against them. I had a hunch that []uint with binary search would be faster than hashing the key and doing lookup in a map[uint]. I was right, but only if the set of uint is smaller than 1000. Below 1000 elements, slices.BinarySearch() is faster, somewhere between 100 and 1000 is the crossover point in this test and after that map[uint]struct{} is faster.

I'm not couning setup time to build the set, just lookup time to do 100_000_000 lookups against the set.

ns/op (lower is better)
set sizearraymap
103.9525.911
1005.8437.104
10007.8996.008
1000010.356.746
10000013.159.547


No comments: