Skip to content

Instantly share code, notes, and snippets.

@derekcollison
Created October 21, 2012 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekcollison/3927749 to your computer and use it in GitHub Desktop.
Save derekcollison/3927749 to your computer and use it in GitHub Desktop.
Fast Hash and HashMap in Go
I have really come to enjoy working in Go. From time to time I expect that the team at Apcera will
need to dip back into C for some things, but I decided to try to push the envelope with Go on making
some faster HashMaps than Go's builtin map. I started with the hash algorithms.
These are some results from my MBA11" core i7.. The fastest Hash algorithms will not work on machines
without support for unaligned access, and I still need to do some cleaning up before we OSS, but so far so good.
I aslo need to implement the fastest one, FNV1A_Hash_Yorikke, from http://http://www.sanmayce.com/Fastest_Hash
I hack SetBytes(1) to get a rough idea of ops/sec, its not actually IO.. So 10 MB/s is 10 Million ops per sec.
======================================
> go test --bench="Hash" -gcflags="-B"
PASS
BenchmarkMurmurHashSmallKey 100000000 14.8 ns/op 67.61 MB/s
BenchmarkNewMurmurHashSmallKey 200000000 9.86 ns/op 101.43 MB/s
BenchmarkFNV1AHashSmallKey 200000000 9.18 ns/op 108.88 MB/s
BenchmarkBernsteinHashSmallKey 200000000 9.70 ns/op 103.13 MB/s
BenchmarkMeiyanHashSmallKey 500000000 7.80 ns/op 128.17 MB/s
BenchmarkMurmurHashMedKey 50000000 66.4 ns/op 15.05 MB/s
BenchmarkNewMurmurHashMedKey 100000000 27.1 ns/op 36.94 MB/s
BenchmarkBernsteinHashMedKey 50000000 47.4 ns/op 21.11 MB/s
BenchmarkMeiyanHashMedKey 100000000 11.1 ns/op 90.38 MB/s
BenchmarkFNV1AHashMedKey 50000000 46.3 ns/op 21.59 MB/s
> go test --bench="Map" -gcflags="-B"
PASS
BenchmarkGoMap 50000000 48.5 ns/op 20.61 MB/s
BenchmarkOurMapGet 100000000 20.8 ns/op 48.09 MB/s
BenchmarkOurMapGetLocked 50000000 35.0 ns/op 28.58 MB/s
BenchmarkGoMapMedKey 20000000 84.5 ns/op 11.84 MB/s
BenchmarkOurMapGetMedKey 20000000 72.8 ns/op 13.73 MB/s
BenchmarkOurMapSet 10000000 156 ns/op 6.39 MB/s
@davecheney
Copy link

Go's maps aren't the fastest, but they are stable, access and insert times are very predictable from 10 elements to 10 million. Can you share some details about the size of the maps you are benchmarking ?

@derekcollison
Copy link
Author

They are fairly small, which is our use case.

@rajakhurram
Copy link

Can you share the code?

@navin89
Copy link

navin89 commented Jan 7, 2019

Can you share the code?

Champion

@navin89
Copy link

navin89 commented Jan 7, 2019

They are fairly small, which is our use case.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment