Skip to content

Instantly share code, notes, and snippets.

@acidlemon
Created February 25, 2015 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidlemon/23cd0b20d2f2c6271e8e to your computer and use it in GitHub Desktop.
Save acidlemon/23cd0b20d2f2c6271e8e to your computer and use it in GitHub Desktop.
package mapbench_test
import "testing"
var N = 1000000
var m1 map[int]int = map[int]int{}
var m2 map[int]int = make(map[int]int, N)
func init() {
for i := 0; i < N; i++ {
m1[i] = i
m2[i] = i
}
}
func BenchmarkMap(b *testing.B) {
for i := 0; i < b.N; i++ {
s := 0
for _, v := range m1 {
s += v
}
}
}
func BenchmarkMapcap(b *testing.B) {
for i := 0; i < b.N; i++ {
s := 0
for _, v := range m2 {
s += v
}
}
}
[2020](*'-')< ~$ go test mapbench_test.go -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkMap 50 27253697 ns/op 0 B/op 0 allocs/op
BenchmarkMapcap 50 26253573 ns/op 0 B/op 0 allocs/op
ok command-line-arguments 3.474s
[2021](*'-')< ~$ go test mapbench_test.go -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkMap 50 26995885 ns/op 0 B/op 0 allocs/op
BenchmarkMapcap 50 26377702 ns/op 0 B/op 0 allocs/op
ok command-line-arguments 3.466s
[2022](*'-')< ~$ go test mapbench_test.go -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkMap 50 27062819 ns/op 0 B/op 0 allocs/op
BenchmarkMapcap 50 26590986 ns/op 0 B/op 0 allocs/op
ok command-line-arguments 3.474s
[2023](*'-')< ~$ go test mapbench_test.go -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkMap 50 27514986 ns/op 0 B/op 0 allocs/op
BenchmarkMapcap 50 26706534 ns/op 0 B/op 0 allocs/op
ok command-line-arguments 3.506s
[2024](*'-')< ~$ go test mapbench_test.go -bench . -benchmem
testing: warning: no tests to run
PASS
BenchmarkMap 50 27476116 ns/op 0 B/op 0 allocs/op
BenchmarkMapcap 50 26293569 ns/op 0 B/op 0 allocs/op
ok command-line-arguments 3.476s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment