Skip to content

Commit 4b4ec6e

Browse files
committed
Add map preallocation benchmarks
1 parent 77a5b5d commit 4b4ec6e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

prealloc_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"go/token"
56
"testing"
67

@@ -125,3 +126,34 @@ func BenchmarkSize200PreallocateCopy(b *testing.B) {
125126
copy(init, existing)
126127
}
127128
}
129+
130+
func BenchmarkMap(b *testing.B) {
131+
benchmarks := []struct {
132+
size int
133+
preallocate bool
134+
}{
135+
{10, false},
136+
{10, true},
137+
{200, false},
138+
{200, true},
139+
}
140+
var m map[int]int
141+
for _, bm := range benchmarks {
142+
no := ""
143+
if !bm.preallocate {
144+
no = "No"
145+
}
146+
b.Run(fmt.Sprintf("Size%d%sPreallocate", bm.size, no), func(b *testing.B) {
147+
for i := 0; i < b.N; i++ {
148+
if bm.preallocate {
149+
m = make(map[int]int, bm.size)
150+
} else {
151+
m = make(map[int]int)
152+
}
153+
for j := 0; j < bm.size; j++ {
154+
m[j] = j
155+
}
156+
}
157+
})
158+
}
159+
}

0 commit comments

Comments
 (0)