22
22
var calibrate = flag.Bool("calibrate", false, "run calibration test")
24
// measure returns the time to run f
25
func measure(f func()) time.Duration {
28
for i := N; i > 0; i-- {
32
return stop.Sub(start) / N
24
func karatsubaLoad(b *testing.B) {
28
// measureKaratsuba returns the time to run a Karatsuba-relevant benchmark
29
// given Karatsuba threshold th.
30
func measureKaratsuba(th int) time.Duration {
31
th, karatsubaThreshold = karatsubaThreshold, th
32
res := testing.Benchmark(karatsubaLoad)
33
karatsubaThreshold = th
34
return time.Duration(res.NsPerOp())
35
37
func computeThresholds() {
37
39
fmt.Printf("(run repeatedly for good results)\n")
39
41
// determine Tk, the work load execution time using basic multiplication
40
karatsubaThreshold = 1e9 // disable karatsuba
41
Tb := measure(benchmarkMulLoad)
42
fmt.Printf("Tb = %dns\n", Tb)
42
Tb := measureKaratsuba(1e9) // th == 1e9 => Karatsuba multiplication disabled
43
fmt.Printf("Tb = %10s\n", Tb)
45
n := 8 // any lower values for the threshold lead to very slow multiplies
49
50
var deltaOld time.Duration
50
for count := -1; count != 0; count-- {
51
for count := -1; count != 0 && th < 128; count-- {
51
52
// determine Tk, the work load execution time using Karatsuba multiplication
52
karatsubaThreshold = n // enable karatsuba
53
Tk := measure(benchmarkMulLoad)
53
Tk := measureKaratsuba(th)
55
55
// improvement over Tb
56
56
delta := (Tb - Tk) * 100 / Tb
58
fmt.Printf("n = %3d Tk = %8dns %4d%%", n, Tk, delta)
58
fmt.Printf("th = %3d Tk = %10s %4d%%", th, Tk, delta)
60
60
// determine break-even point
61
61
if Tk < Tb && th1 < 0 {
63
63
fmt.Print(" break-even point")
66
66
// determine diminishing return
67
67
if 0 < delta && delta < deltaOld && th2 < 0 {
69
69
fmt.Print(" diminishing return")