Commit 07f0c19a authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

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/23424Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 23cb8864
......@@ -5,6 +5,7 @@
package big
import (
"fmt"
"math/rand"
"testing"
)
......@@ -118,28 +119,36 @@ func rndV(n int) []Word {
return v
}
func benchmarkFunVV(b *testing.B, f funVV, n int) {
x := rndV(n)
y := rndV(n)
z := make([]Word, n)
b.SetBytes(int64(n * _W))
b.ResetTimer()
for i := 0; i < b.N; i++ {
f(z, x, y)
var benchSizes = []struct {
name string
n int
}{
{"1", 1},
{"2", 2},
{"3", 3},
{"4", 4},
{"5", 5},
{"1e1", 1e1},
{"1e2", 1e2},
{"1e3", 1e3},
{"1e4", 1e4},
{"1e5", 1e5},
}
func BenchmarkAddVV(b *testing.B) {
for _, tc := range benchSizes {
x := rndV(tc.n)
y := rndV(tc.n)
z := make([]Word, tc.n)
b.Run(fmt.Sprint(tc.name), func(b *testing.B) {
b.SetBytes(int64(tc.n * _W))
for i := 0; i < b.N; i++ {
addVV(z, x, y)
}
})
}
}
func BenchmarkAddVV_1(b *testing.B) { benchmarkFunVV(b, addVV, 1) }
func BenchmarkAddVV_2(b *testing.B) { benchmarkFunVV(b, addVV, 2) }
func BenchmarkAddVV_3(b *testing.B) { benchmarkFunVV(b, addVV, 3) }
func BenchmarkAddVV_4(b *testing.B) { benchmarkFunVV(b, addVV, 4) }
func BenchmarkAddVV_5(b *testing.B) { benchmarkFunVV(b, addVV, 5) }
func BenchmarkAddVV_1e1(b *testing.B) { benchmarkFunVV(b, addVV, 1e1) }
func BenchmarkAddVV_1e2(b *testing.B) { benchmarkFunVV(b, addVV, 1e2) }
func BenchmarkAddVV_1e3(b *testing.B) { benchmarkFunVV(b, addVV, 1e3) }
func BenchmarkAddVV_1e4(b *testing.B) { benchmarkFunVV(b, addVV, 1e4) }
func BenchmarkAddVV_1e5(b *testing.B) { benchmarkFunVV(b, addVV, 1e5) }
type funVW func(z, x []Word, y Word) (c Word)
type argVW struct {
z, x nat
......@@ -236,28 +245,20 @@ func TestFunVW(t *testing.T) {
}
}
func benchmarkFunVW(b *testing.B, f funVW, n int) {
x := rndV(n)
y := rndW()
z := make([]Word, n)
b.SetBytes(int64(n * _S))
b.ResetTimer()
for i := 0; i < b.N; i++ {
f(z, x, y)
func BenchmarkAddVW(b *testing.B) {
for _, tc := range benchSizes {
x := rndV(tc.n)
y := rndW()
z := make([]Word, tc.n)
b.Run(fmt.Sprint(tc.name), func(b *testing.B) {
b.SetBytes(int64(tc.n * _S))
for i := 0; i < b.N; i++ {
addVW(z, x, y)
}
})
}
}
func BenchmarkAddVW_1(b *testing.B) { benchmarkFunVW(b, addVW, 1) }
func BenchmarkAddVW_2(b *testing.B) { benchmarkFunVW(b, addVW, 2) }
func BenchmarkAddVW_3(b *testing.B) { benchmarkFunVW(b, addVW, 3) }
func BenchmarkAddVW_4(b *testing.B) { benchmarkFunVW(b, addVW, 4) }
func BenchmarkAddVW_5(b *testing.B) { benchmarkFunVW(b, addVW, 5) }
func BenchmarkAddVW_1e1(b *testing.B) { benchmarkFunVW(b, addVW, 1e1) }
func BenchmarkAddVW_1e2(b *testing.B) { benchmarkFunVW(b, addVW, 1e2) }
func BenchmarkAddVW_1e3(b *testing.B) { benchmarkFunVW(b, addVW, 1e3) }
func BenchmarkAddVW_1e4(b *testing.B) { benchmarkFunVW(b, addVW, 1e4) }
func BenchmarkAddVW_1e5(b *testing.B) { benchmarkFunVW(b, addVW, 1e5) }
type funVWW func(z, x []Word, y, r Word) (c Word)
type argVWW struct {
z, x nat
......@@ -382,28 +383,20 @@ func TestMulAddWWW(t *testing.T) {
}
}
func benchmarkAddMulVVW(b *testing.B, n int) {
x := rndV(n)
y := rndW()
z := make([]Word, n)
b.SetBytes(int64(n * _W))
b.ResetTimer()
for i := 0; i < b.N; i++ {
addMulVVW(z, x, y)
func BenchmarkAddMulVVW(b *testing.B) {
for _, tc := range benchSizes {
x := rndV(tc.n)
y := rndW()
z := make([]Word, tc.n)
b.Run(fmt.Sprint(tc.n), func(b *testing.B) {
b.SetBytes(int64(tc.n * _W))
for i := 0; i < b.N; i++ {
addMulVVW(z, x, y)
}
})
}
}
func BenchmarkAddMulVVW_1(b *testing.B) { benchmarkAddMulVVW(b, 1) }
func BenchmarkAddMulVVW_2(b *testing.B) { benchmarkAddMulVVW(b, 2) }
func BenchmarkAddMulVVW_3(b *testing.B) { benchmarkAddMulVVW(b, 3) }
func BenchmarkAddMulVVW_4(b *testing.B) { benchmarkAddMulVVW(b, 4) }
func BenchmarkAddMulVVW_5(b *testing.B) { benchmarkAddMulVVW(b, 5) }
func BenchmarkAddMulVVW_1e1(b *testing.B) { benchmarkAddMulVVW(b, 1e1) }
func BenchmarkAddMulVVW_1e2(b *testing.B) { benchmarkAddMulVVW(b, 1e2) }
func BenchmarkAddMulVVW_1e3(b *testing.B) { benchmarkAddMulVVW(b, 1e3) }
func BenchmarkAddMulVVW_1e4(b *testing.B) { benchmarkAddMulVVW(b, 1e4) }
func BenchmarkAddMulVVW_1e5(b *testing.B) { benchmarkAddMulVVW(b, 1e5) }
func testWordBitLen(t *testing.T, fname string, f func(Word) int) {
for i := 0; i <= _W; i++ {
x := Word(1) << uint(i-1) // i == 0 => x == 0
......@@ -420,23 +413,15 @@ func TestWordBitLen(t *testing.T) {
}
// runs b.N iterations of bitLen called on a Word containing (1 << nbits)-1.
func benchmarkBitLenN(b *testing.B, nbits uint) {
testword := Word((uint64(1) << nbits) - 1)
for i := 0; i < b.N; i++ {
bitLen(testword)
func BenchmarkBitLen(b *testing.B) {
// Individual bitLen tests. Numbers chosen to examine both sides
// of powers-of-two boundaries.
for _, nbits := range []uint{0, 1, 2, 3, 4, 5, 8, 9, 16, 17, 31} {
testword := Word((uint64(1) << nbits) - 1)
b.Run(fmt.Sprint(nbits), func(b *testing.B) {
for i := 0; i < b.N; i++ {
bitLen(testword)
}
})
}
}
// Individual bitLen tests. Numbers chosen to examine both sides
// of powers-of-two boundaries.
func BenchmarkBitLen0(b *testing.B) { benchmarkBitLenN(b, 0) }
func BenchmarkBitLen1(b *testing.B) { benchmarkBitLenN(b, 1) }
func BenchmarkBitLen2(b *testing.B) { benchmarkBitLenN(b, 2) }
func BenchmarkBitLen3(b *testing.B) { benchmarkBitLenN(b, 3) }
func BenchmarkBitLen4(b *testing.B) { benchmarkBitLenN(b, 4) }
func BenchmarkBitLen5(b *testing.B) { benchmarkBitLenN(b, 5) }
func BenchmarkBitLen8(b *testing.B) { benchmarkBitLenN(b, 8) }
func BenchmarkBitLen9(b *testing.B) { benchmarkBitLenN(b, 9) }
func BenchmarkBitLen16(b *testing.B) { benchmarkBitLenN(b, 16) }
func BenchmarkBitLen17(b *testing.B) { benchmarkBitLenN(b, 17) }
func BenchmarkBitLen31(b *testing.B) { benchmarkBitLenN(b, 31) }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment