-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_test.go
74 lines (59 loc) · 1.9 KB
/
benchmark_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
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019, LightStep Inc.
//
// The benchmark results point to a performance drop when the
// largeHeap starts to be used because of interface conversions in and
// out of the heap, primarily due to the heap interface. This
// suggests room for improvement by avoiding the built-in heap.
/*
BenchmarkAdd_Norm_100-8 37540165 32.1 ns/op 8 B/op 0 allocs/op
BenchmarkAdd_Norm_10000-8 39850280 30.6 ns/op 8 B/op 0 allocs/op
BenchmarkAdd_Norm_1000000-8 7958835 183 ns/op 52 B/op 0 allocs/op
BenchmarkAdd_Exp_100-8 41565934 28.5 ns/op 8 B/op 0 allocs/op
BenchmarkAdd_Exp_10000-8 43622184 29.2 ns/op 8 B/op 0 allocs/op
BenchmarkAdd_Exp_1000000-8 8103663 220 ns/op 55 B/op 0 allocs/op
*/
package varopt_test
import (
"math"
"math/rand"
"testing"
"github.com/lightstep/varopt"
)
func normValue(rnd *rand.Rand) float64 {
return 1 + math.Abs(rnd.NormFloat64())
}
func expValue(rnd *rand.Rand) float64 {
return rnd.ExpFloat64()
}
func BenchmarkAdd_Norm_100(b *testing.B) {
benchmarkAdd(b, 100, normValue)
}
func BenchmarkAdd_Norm_10000(b *testing.B) {
benchmarkAdd(b, 10000, normValue)
}
func BenchmarkAdd_Norm_1000000(b *testing.B) {
benchmarkAdd(b, 1000000, normValue)
}
func BenchmarkAdd_Exp_100(b *testing.B) {
benchmarkAdd(b, 100, expValue)
}
func BenchmarkAdd_Exp_10000(b *testing.B) {
benchmarkAdd(b, 10000, expValue)
}
func BenchmarkAdd_Exp_1000000(b *testing.B) {
benchmarkAdd(b, 1000000, expValue)
}
type thing struct{}
func benchmarkAdd(b *testing.B, size int, f func(rnd *rand.Rand) float64) {
b.ReportAllocs()
rnd := rand.New(rand.NewSource(3331))
v := varopt.New[thing](size, rnd)
weights := make([]float64, b.N)
for i := 0; i < b.N; i++ {
weights[i] = f(rnd)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
v.Add(thing{}, weights[i])
}
}