-
Notifications
You must be signed in to change notification settings - Fork 4
/
uint32_test.go
57 lines (52 loc) · 1004 Bytes
/
uint32_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
package ipx
import (
"math/rand"
"net"
"testing"
"time"
)
func BenchmarkUint32(b *testing.B) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
left, right := r.Uint32(), r.Uint32()
shift := uint(r.Intn(32))
ip4 := make(net.IP, 16)
copy(ip4, net.IPv4zero)
_, _ = r.Read(ip4[12:])
blank := make(net.IP, 16)
copy(blank, net.IPv4zero)
b.Run("new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = to32(ip4)
}
})
b.Run("from", func(b *testing.B) {
for i := 0; i < b.N; i++ {
from32(left, blank)
}
})
b.Run("add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = left + right
}
})
b.Run("minus", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = left - right
}
})
b.Run("lsh", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = left << shift
}
})
b.Run("rsh", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = left >> shift
}
})
b.Run("cmp", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = left < right
}
})
}