1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file prints execution times for the Mul benchmark
6 // given different Karatsuba thresholds. The result may be
7 // used to manually fine-tune the threshold constant. The
8 // results are somewhat fragile; use repeated runs to get
11 // Usage: go test -run=TestCalibrate -calibrate
22 var calibrate = flag.Bool("calibrate", false, "run calibration test")
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())
37 func computeThresholds() {
38 fmt.Printf("Multiplication times for varying Karatsuba thresholds\n")
39 fmt.Printf("(run repeatedly for good results)\n")
41 // determine Tk, the work load execution time using basic multiplication
42 Tb := measureKaratsuba(1e9) // th == 1e9 => Karatsuba multiplication disabled
43 fmt.Printf("Tb = %10s\n", Tb)
50 var deltaOld time.Duration
51 for count := -1; count != 0 && th < 128; count-- {
52 // determine Tk, the work load execution time using Karatsuba multiplication
53 Tk := measureKaratsuba(th)
55 // improvement over Tb
56 delta := (Tb - Tk) * 100 / Tb
58 fmt.Printf("th = %3d Tk = %10s %4d%%", th, Tk, delta)
60 // determine break-even point
61 if Tk < Tb && th1 < 0 {
63 fmt.Print(" break-even point")
66 // determine diminishing return
67 if 0 < delta && delta < deltaOld && th2 < 0 {
69 fmt.Print(" diminishing return")
76 if th1 >= 0 && th2 >= 0 && count < 0 {
77 count = 10 // this many extra measurements after we got both thresholds
84 func TestCalibrate(t *testing.T) {