|
| 1 | +// Copyright 2019, LightStep Inc. |
| 2 | +// |
| 3 | +// The benchmark results point to a performance drop when the |
| 4 | +// largeHeap starts to be used because of interface conversions in and |
| 5 | +// out of the heap, primarily due to the heap interface. This |
| 6 | +// suggests room for improvement by avoiding the built-in heap. |
| 7 | + |
| 8 | +/* |
| 9 | +BenchmarkAdd_Norm_100-8 37540165 32.1 ns/op 8 B/op 0 allocs/op |
| 10 | +BenchmarkAdd_Norm_10000-8 39850280 30.6 ns/op 8 B/op 0 allocs/op |
| 11 | +BenchmarkAdd_Norm_1000000-8 7958835 183 ns/op 52 B/op 0 allocs/op |
| 12 | +BenchmarkAdd_Exp_100-8 41565934 28.5 ns/op 8 B/op 0 allocs/op |
| 13 | +BenchmarkAdd_Exp_10000-8 43622184 29.2 ns/op 8 B/op 0 allocs/op |
| 14 | +BenchmarkAdd_Exp_1000000-8 8103663 220 ns/op 55 B/op 0 allocs/op |
| 15 | +*/ |
| 16 | + |
| 17 | +package varopt_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "math" |
| 21 | + "math/rand" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/lightstep/varopt" |
| 25 | +) |
| 26 | + |
| 27 | +func normValue(rnd *rand.Rand) float64 { |
| 28 | + return 1 + math.Abs(rnd.NormFloat64()) |
| 29 | +} |
| 30 | + |
| 31 | +func expValue(rnd *rand.Rand) float64 { |
| 32 | + return rnd.ExpFloat64() |
| 33 | +} |
| 34 | + |
| 35 | +func BenchmarkAdd_Norm_100(b *testing.B) { |
| 36 | + benchmarkAdd(b, 100, normValue) |
| 37 | +} |
| 38 | + |
| 39 | +func BenchmarkAdd_Norm_10000(b *testing.B) { |
| 40 | + benchmarkAdd(b, 10000, normValue) |
| 41 | +} |
| 42 | + |
| 43 | +func BenchmarkAdd_Norm_1000000(b *testing.B) { |
| 44 | + benchmarkAdd(b, 1000000, normValue) |
| 45 | +} |
| 46 | + |
| 47 | +func BenchmarkAdd_Exp_100(b *testing.B) { |
| 48 | + benchmarkAdd(b, 100, expValue) |
| 49 | +} |
| 50 | + |
| 51 | +func BenchmarkAdd_Exp_10000(b *testing.B) { |
| 52 | + benchmarkAdd(b, 10000, expValue) |
| 53 | +} |
| 54 | + |
| 55 | +func BenchmarkAdd_Exp_1000000(b *testing.B) { |
| 56 | + benchmarkAdd(b, 1000000, expValue) |
| 57 | +} |
| 58 | + |
| 59 | +func benchmarkAdd(b *testing.B, size int, f func(rnd *rand.Rand) float64) { |
| 60 | + b.ReportAllocs() |
| 61 | + rnd := rand.New(rand.NewSource(3331)) |
| 62 | + v := varopt.New(size, rnd) |
| 63 | + weights := make([]float64, b.N) |
| 64 | + for i := 0; i < b.N; i++ { |
| 65 | + weights[i] = f(rnd) |
| 66 | + } |
| 67 | + |
| 68 | + b.StartTimer() |
| 69 | + for i := 0; i < b.N; i++ { |
| 70 | + v.Add(nil, weights[i]) |
| 71 | + } |
| 72 | +} |
0 commit comments