Skip to content

Instantly share code, notes, and snippets.

@totoleo
Created June 24, 2020 11:53
Show Gist options
  • Save totoleo/7ff1f5221a6d5fe1136a32e0a9bf8bf9 to your computer and use it in GitHub Desktop.
Save totoleo/7ff1f5221a6d5fe1136a32e0a9bf8bf9 to your computer and use it in GitHub Desktop.
测试 map 的 assign 性能
goos: darwin
goarch: amd64
pkg: perf
BenchmarkMapAssign
BenchmarkMapAssign/struct{}
BenchmarkMapAssign/struct{}-4 342277 3384 ns/op 1658 B/op 7 allocs/op
BenchmarkMapAssign/struct{}#01
BenchmarkMapAssign/struct{}#01-4 176937 6258 ns/op 3071 B/op 17 allocs/op
BenchmarkMapAssign/bool
BenchmarkMapAssign/bool-4 377792 3118 ns/op 3109 B/op 2 allocs/op
BenchmarkMapAssign/bool#01
BenchmarkMapAssign/bool#01-4 329834 3270 ns/op 1815 B/op 7 allocs/op
BenchmarkMapAssign/bool#02
BenchmarkMapAssign/bool#02-4 177361 6254 ns/op 3380 B/op 17 allocs/op
PASS
package main
import (
"testing"
)
func BenchmarkMapAssign(b *testing.B) {
const max = 100
b.Run("struct{}", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dupDocs := make(map[int]struct{},max*4/4)
for i := 0; i < max; i++ {
dupDocs[i]= struct{}{}
}
}
})
b.Run("struct{}", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dupDocs := make(map[int]struct{},0)
for i := 0; i < max; i++ {
dupDocs[i]= struct{}{}
}
}
})
b.Run("bool", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dupDocs := make(map[int]byte,max*4/2)
for i := 0; i < max; i++ {
dupDocs[i]= 1
}
}
})
b.Run("bool", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dupDocs := make(map[int]byte,max*4/4)
for i := 0; i < max; i++ {
dupDocs[i]= 1
}
}
})
b.Run("bool", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dupDocs := make(map[int]byte)
for i := 0; i < max; i++ {
dupDocs[i]= 1
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment