Skip to content

Commit

Permalink
Merge pull request #23 from scop/map-benchmarks
Browse files Browse the repository at this point in the history
Add map preallocation benchmarks
  • Loading branch information
alexkohler authored Apr 11, 2024
2 parents dbc83e7 + 4b4ec6e commit deab4f5
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions prealloc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"go/token"
"testing"

Expand Down Expand Up @@ -125,3 +126,34 @@ func BenchmarkSize200PreallocateCopy(b *testing.B) {
copy(init, existing)
}
}

func BenchmarkMap(b *testing.B) {
benchmarks := []struct {
size int
preallocate bool
}{
{10, false},
{10, true},
{200, false},
{200, true},
}
var m map[int]int
for _, bm := range benchmarks {
no := ""
if !bm.preallocate {
no = "No"
}
b.Run(fmt.Sprintf("Size%d%sPreallocate", bm.size, no), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if bm.preallocate {
m = make(map[int]int, bm.size)
} else {
m = make(map[int]int)
}
for j := 0; j < bm.size; j++ {
m[j] = j
}
}
})
}
}

0 comments on commit deab4f5

Please sign in to comment.