-
Notifications
You must be signed in to change notification settings - Fork 1
/
memoize_with_err_bench_test.go
61 lines (48 loc) · 1.12 KB
/
memoize_with_err_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package memoize
import (
"fmt"
"math"
"testing"
)
func BenchmarkMemoizerWithErr(b *testing.B) {
for i := 1; i < maxKeySpace; i++ {
keysN := math.Pow(10, float64(i))
name := fmt.Sprintf("Keys:%.f", keysN)
b.Run(name, func(b *testing.B) {
mem := NewWithErr(func(k string) (int, error) { return len(k), nil })
keys := []string{}
for i := range int64(keysN) {
keys = append(keys, string(fmt.Sprintf("Key%d", i)))
}
b.ResetTimer()
for i := range b.N {
k := keys[i%len(keys)]
r, _ := mem(k)
returns = r
}
})
}
}
func BenchmarkMemoizerWithErrParallel(b *testing.B) {
for i := 1; i < maxKeySpace; i++ {
keysN := math.Pow(10, float64(i))
name := fmt.Sprintf("Keys:%.f", keysN)
b.Run(name, func(b *testing.B) {
mem := NewWithErr(func(k string) (int, error) { return len(k), nil })
keys := []string{}
for i := range int64(keysN) {
keys = append(keys, string(fmt.Sprintf("Key%d", i)))
}
b.ResetTimer()
b.RunParallel(func(b *testing.PB) {
i := 0
for b.Next() {
k := keys[i%len(keys)]
r, _ := mem(k)
returns = r
i++
}
})
})
}
}