1
2
3
4
5 package test
6
7 import "testing"
8
9 var globl int64
10 var globl32 int32
11
12 func BenchmarkLoadAdd(b *testing.B) {
13 x := make([]int64, 1024)
14 y := make([]int64, 1024)
15 for i := 0; i < b.N; i++ {
16 var s int64
17 for i := range x {
18 s ^= x[i] + y[i]
19 }
20 globl = s
21 }
22 }
23
24
25 func BenchmarkExtShift(b *testing.B) {
26 x := make([]int32, 1024)
27 for i := 0; i < b.N; i++ {
28 var s int64
29 for i := range x {
30 s ^= int64(x[i]+32) * 8
31 }
32 globl = s
33 }
34 }
35
36 func BenchmarkModify(b *testing.B) {
37 a := make([]int64, 1024)
38 v := globl
39 for i := 0; i < b.N; i++ {
40 for j := range a {
41 a[j] += v
42 }
43 }
44 }
45
46 func BenchmarkMullImm(b *testing.B) {
47 x := make([]int32, 1024)
48 for i := 0; i < b.N; i++ {
49 var s int32
50 for i := range x {
51 s += x[i] * 100
52 }
53 globl32 = s
54 }
55 }
56
57 func BenchmarkConstModify(b *testing.B) {
58 a := make([]int64, 1024)
59 for i := 0; i < b.N; i++ {
60 for j := range a {
61 a[j] += 3
62 }
63 }
64 }
65
66 func BenchmarkBitSet(b *testing.B) {
67 const N = 64 * 8
68 a := make([]uint64, N/64)
69 for i := 0; i < b.N; i++ {
70 for j := uint64(0); j < N; j++ {
71 a[j/64] |= 1 << (j % 64)
72 }
73 }
74 }
75
76 func BenchmarkBitClear(b *testing.B) {
77 const N = 64 * 8
78 a := make([]uint64, N/64)
79 for i := 0; i < b.N; i++ {
80 for j := uint64(0); j < N; j++ {
81 a[j/64] &^= 1 << (j % 64)
82 }
83 }
84 }
85
86 func BenchmarkBitToggle(b *testing.B) {
87 const N = 64 * 8
88 a := make([]uint64, N/64)
89 for i := 0; i < b.N; i++ {
90 for j := uint64(0); j < N; j++ {
91 a[j/64] ^= 1 << (j % 64)
92 }
93 }
94 }
95
96 func BenchmarkBitSetConst(b *testing.B) {
97 const N = 64
98 a := make([]uint64, N)
99 for i := 0; i < b.N; i++ {
100 for j := range a {
101 a[j] |= 1 << 37
102 }
103 }
104 }
105
106 func BenchmarkBitClearConst(b *testing.B) {
107 const N = 64
108 a := make([]uint64, N)
109 for i := 0; i < b.N; i++ {
110 for j := range a {
111 a[j] &^= 1 << 37
112 }
113 }
114 }
115
116 func BenchmarkBitToggleConst(b *testing.B) {
117 const N = 64
118 a := make([]uint64, N)
119 for i := 0; i < b.N; i++ {
120 for j := range a {
121 a[j] ^= 1 << 37
122 }
123 }
124 }
125
View as plain text