Skip to content

Commit 07f0c19

Browse files
committed
math/big: use run for benchmarks
shortens code and gives an example of the use of Run. Change-Id: I75ffaf762218a589274b4b62e19022e31e805d1b Reviewed-on: https://go-review.googlesource.com/23424 Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Marcel van Lohuizen <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 23cb886 commit 07f0c19

File tree

1 file changed

+60
-75
lines changed

1 file changed

+60
-75
lines changed

src/math/big/arith_test.go

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package big
66

77
import (
8+
"fmt"
89
"math/rand"
910
"testing"
1011
)
@@ -118,28 +119,36 @@ func rndV(n int) []Word {
118119
return v
119120
}
120121

121-
func benchmarkFunVV(b *testing.B, f funVV, n int) {
122-
x := rndV(n)
123-
y := rndV(n)
124-
z := make([]Word, n)
125-
b.SetBytes(int64(n * _W))
126-
b.ResetTimer()
127-
for i := 0; i < b.N; i++ {
128-
f(z, x, y)
122+
var benchSizes = []struct {
123+
name string
124+
n int
125+
}{
126+
{"1", 1},
127+
{"2", 2},
128+
{"3", 3},
129+
{"4", 4},
130+
{"5", 5},
131+
{"1e1", 1e1},
132+
{"1e2", 1e2},
133+
{"1e3", 1e3},
134+
{"1e4", 1e4},
135+
{"1e5", 1e5},
136+
}
137+
138+
func BenchmarkAddVV(b *testing.B) {
139+
for _, tc := range benchSizes {
140+
x := rndV(tc.n)
141+
y := rndV(tc.n)
142+
z := make([]Word, tc.n)
143+
b.Run(fmt.Sprint(tc.name), func(b *testing.B) {
144+
b.SetBytes(int64(tc.n * _W))
145+
for i := 0; i < b.N; i++ {
146+
addVV(z, x, y)
147+
}
148+
})
129149
}
130150
}
131151

132-
func BenchmarkAddVV_1(b *testing.B) { benchmarkFunVV(b, addVV, 1) }
133-
func BenchmarkAddVV_2(b *testing.B) { benchmarkFunVV(b, addVV, 2) }
134-
func BenchmarkAddVV_3(b *testing.B) { benchmarkFunVV(b, addVV, 3) }
135-
func BenchmarkAddVV_4(b *testing.B) { benchmarkFunVV(b, addVV, 4) }
136-
func BenchmarkAddVV_5(b *testing.B) { benchmarkFunVV(b, addVV, 5) }
137-
func BenchmarkAddVV_1e1(b *testing.B) { benchmarkFunVV(b, addVV, 1e1) }
138-
func BenchmarkAddVV_1e2(b *testing.B) { benchmarkFunVV(b, addVV, 1e2) }
139-
func BenchmarkAddVV_1e3(b *testing.B) { benchmarkFunVV(b, addVV, 1e3) }
140-
func BenchmarkAddVV_1e4(b *testing.B) { benchmarkFunVV(b, addVV, 1e4) }
141-
func BenchmarkAddVV_1e5(b *testing.B) { benchmarkFunVV(b, addVV, 1e5) }
142-
143152
type funVW func(z, x []Word, y Word) (c Word)
144153
type argVW struct {
145154
z, x nat
@@ -236,28 +245,20 @@ func TestFunVW(t *testing.T) {
236245
}
237246
}
238247

239-
func benchmarkFunVW(b *testing.B, f funVW, n int) {
240-
x := rndV(n)
241-
y := rndW()
242-
z := make([]Word, n)
243-
b.SetBytes(int64(n * _S))
244-
b.ResetTimer()
245-
for i := 0; i < b.N; i++ {
246-
f(z, x, y)
248+
func BenchmarkAddVW(b *testing.B) {
249+
for _, tc := range benchSizes {
250+
x := rndV(tc.n)
251+
y := rndW()
252+
z := make([]Word, tc.n)
253+
b.Run(fmt.Sprint(tc.name), func(b *testing.B) {
254+
b.SetBytes(int64(tc.n * _S))
255+
for i := 0; i < b.N; i++ {
256+
addVW(z, x, y)
257+
}
258+
})
247259
}
248260
}
249261

250-
func BenchmarkAddVW_1(b *testing.B) { benchmarkFunVW(b, addVW, 1) }
251-
func BenchmarkAddVW_2(b *testing.B) { benchmarkFunVW(b, addVW, 2) }
252-
func BenchmarkAddVW_3(b *testing.B) { benchmarkFunVW(b, addVW, 3) }
253-
func BenchmarkAddVW_4(b *testing.B) { benchmarkFunVW(b, addVW, 4) }
254-
func BenchmarkAddVW_5(b *testing.B) { benchmarkFunVW(b, addVW, 5) }
255-
func BenchmarkAddVW_1e1(b *testing.B) { benchmarkFunVW(b, addVW, 1e1) }
256-
func BenchmarkAddVW_1e2(b *testing.B) { benchmarkFunVW(b, addVW, 1e2) }
257-
func BenchmarkAddVW_1e3(b *testing.B) { benchmarkFunVW(b, addVW, 1e3) }
258-
func BenchmarkAddVW_1e4(b *testing.B) { benchmarkFunVW(b, addVW, 1e4) }
259-
func BenchmarkAddVW_1e5(b *testing.B) { benchmarkFunVW(b, addVW, 1e5) }
260-
261262
type funVWW func(z, x []Word, y, r Word) (c Word)
262263
type argVWW struct {
263264
z, x nat
@@ -382,28 +383,20 @@ func TestMulAddWWW(t *testing.T) {
382383
}
383384
}
384385

385-
func benchmarkAddMulVVW(b *testing.B, n int) {
386-
x := rndV(n)
387-
y := rndW()
388-
z := make([]Word, n)
389-
b.SetBytes(int64(n * _W))
390-
b.ResetTimer()
391-
for i := 0; i < b.N; i++ {
392-
addMulVVW(z, x, y)
386+
func BenchmarkAddMulVVW(b *testing.B) {
387+
for _, tc := range benchSizes {
388+
x := rndV(tc.n)
389+
y := rndW()
390+
z := make([]Word, tc.n)
391+
b.Run(fmt.Sprint(tc.n), func(b *testing.B) {
392+
b.SetBytes(int64(tc.n * _W))
393+
for i := 0; i < b.N; i++ {
394+
addMulVVW(z, x, y)
395+
}
396+
})
393397
}
394398
}
395399

396-
func BenchmarkAddMulVVW_1(b *testing.B) { benchmarkAddMulVVW(b, 1) }
397-
func BenchmarkAddMulVVW_2(b *testing.B) { benchmarkAddMulVVW(b, 2) }
398-
func BenchmarkAddMulVVW_3(b *testing.B) { benchmarkAddMulVVW(b, 3) }
399-
func BenchmarkAddMulVVW_4(b *testing.B) { benchmarkAddMulVVW(b, 4) }
400-
func BenchmarkAddMulVVW_5(b *testing.B) { benchmarkAddMulVVW(b, 5) }
401-
func BenchmarkAddMulVVW_1e1(b *testing.B) { benchmarkAddMulVVW(b, 1e1) }
402-
func BenchmarkAddMulVVW_1e2(b *testing.B) { benchmarkAddMulVVW(b, 1e2) }
403-
func BenchmarkAddMulVVW_1e3(b *testing.B) { benchmarkAddMulVVW(b, 1e3) }
404-
func BenchmarkAddMulVVW_1e4(b *testing.B) { benchmarkAddMulVVW(b, 1e4) }
405-
func BenchmarkAddMulVVW_1e5(b *testing.B) { benchmarkAddMulVVW(b, 1e5) }
406-
407400
func testWordBitLen(t *testing.T, fname string, f func(Word) int) {
408401
for i := 0; i <= _W; i++ {
409402
x := Word(1) << uint(i-1) // i == 0 => x == 0
@@ -420,23 +413,15 @@ func TestWordBitLen(t *testing.T) {
420413
}
421414

422415
// runs b.N iterations of bitLen called on a Word containing (1 << nbits)-1.
423-
func benchmarkBitLenN(b *testing.B, nbits uint) {
424-
testword := Word((uint64(1) << nbits) - 1)
425-
for i := 0; i < b.N; i++ {
426-
bitLen(testword)
416+
func BenchmarkBitLen(b *testing.B) {
417+
// Individual bitLen tests. Numbers chosen to examine both sides
418+
// of powers-of-two boundaries.
419+
for _, nbits := range []uint{0, 1, 2, 3, 4, 5, 8, 9, 16, 17, 31} {
420+
testword := Word((uint64(1) << nbits) - 1)
421+
b.Run(fmt.Sprint(nbits), func(b *testing.B) {
422+
for i := 0; i < b.N; i++ {
423+
bitLen(testword)
424+
}
425+
})
427426
}
428427
}
429-
430-
// Individual bitLen tests. Numbers chosen to examine both sides
431-
// of powers-of-two boundaries.
432-
func BenchmarkBitLen0(b *testing.B) { benchmarkBitLenN(b, 0) }
433-
func BenchmarkBitLen1(b *testing.B) { benchmarkBitLenN(b, 1) }
434-
func BenchmarkBitLen2(b *testing.B) { benchmarkBitLenN(b, 2) }
435-
func BenchmarkBitLen3(b *testing.B) { benchmarkBitLenN(b, 3) }
436-
func BenchmarkBitLen4(b *testing.B) { benchmarkBitLenN(b, 4) }
437-
func BenchmarkBitLen5(b *testing.B) { benchmarkBitLenN(b, 5) }
438-
func BenchmarkBitLen8(b *testing.B) { benchmarkBitLenN(b, 8) }
439-
func BenchmarkBitLen9(b *testing.B) { benchmarkBitLenN(b, 9) }
440-
func BenchmarkBitLen16(b *testing.B) { benchmarkBitLenN(b, 16) }
441-
func BenchmarkBitLen17(b *testing.B) { benchmarkBitLenN(b, 17) }
442-
func BenchmarkBitLen31(b *testing.B) { benchmarkBitLenN(b, 31) }

0 commit comments

Comments
 (0)