-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench_test.go
65 lines (60 loc) · 1.02 KB
/
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
62
63
64
65
package gocast
import (
"testing"
"github.com/stretchr/testify/assert"
)
func bench1(v any) int {
switch iv := v.(type) {
case int:
return int(iv)
case int8:
return int(iv)
case int16:
return int(iv)
case int32:
return int(iv)
}
return 0
}
func bench2[T Numeric](v T) int {
switch iv := any(v).(type) {
case int:
return int(iv)
case int8:
return int(iv)
case int16:
return int(iv)
case int32:
return int(iv)
}
return 0
}
func BenchmarkApproachTest(b *testing.B) {
var vals = []struct {
source int32
target int
}{
{source: 1, target: 1},
{source: 10, target: 10},
{source: 100, target: 100},
}
b.ReportAllocs()
b.Run("bench1", func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
for _, v := range vals {
assert.Equal(b, v.target, bench1(v.source))
}
}
})
})
b.Run("bench2", func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
for _, v := range vals {
assert.Equal(b, v.target, bench2(v.source))
}
}
})
})
}