-
-
Save faiface/1c9f2206bb08b8bc62c7048d40119dfa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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