Skip to content

Instantly share code, notes, and snippets.

@faiface
Created August 11, 2017 23:21
Show Gist options
  • Save faiface/1c9f2206bb08b8bc62c7048d40119dfa to your computer and use it in GitHub Desktop.
Save faiface/1c9f2206bb08b8bc62c7048d40119dfa to your computer and use it in GitHub Desktop.
package test_test
import (
"context"
"fmt"
"math/rand"
"testing"
)
func BenchmarkMap(b *testing.B) {
for _, size := range []int{1, 5, 10, 100, 1000} {
m := make(map[int]int)
for i := 0; i < size; i++ {
m[i] = rand.Int()
}
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = m[rand.Intn(size)]
}
})
}
}
func BenchmarkContextValue(b *testing.B) {
for _, size := range []int{1, 5, 10, 100, 1000} {
ctx := context.Background()
for i := 0; i < size; i++ {
ctx = context.WithValue(ctx, i, rand.Int())
}
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ctx.Value(rand.Intn(size))
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment