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: gotest -calibrate
22 var calibrate = flag.Bool("calibrate", false, "run calibration test")
24 // measure returns the time to run f
25 func measure(f func()) int64 {
27 start := time.Nanoseconds()
28 for i := N; i > 0; i-- {
31 stop := time.Nanoseconds()
32 return (stop - start) / N
35 func computeThresholds() {
36 fmt.Printf("Multiplication times for varying Karatsuba thresholds\n")
37 fmt.Printf("(run repeatedly for good results)\n")
39 // 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)
45 n := 8 // any lower values for the threshold lead to very slow multiplies
50 for count := -1; count != 0; count-- {
51 // determine Tk, the work load execution time using Karatsuba multiplication
52 karatsubaThreshold = n // enable karatsuba
53 Tk := measure(benchmarkMulLoad)
55 // improvement over Tb
56 delta := (Tb - Tk) * 100 / Tb
58 fmt.Printf("n = %3d Tk = %8dns %4d%%", n, 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 = 20 // this many extra measurements after we got both thresholds
84 func TestCalibrate(t *testing.T) {