github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/big/calibrate_test.go (about) 1 // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/big/calibrate_test.go 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file prints execution times for the Mul benchmark 8 // given different Karatsuba thresholds. The result may be 9 // used to manually fine-tune the threshold constant. The 10 // results are somewhat fragile; use repeated runs to get 11 // a clear picture. 12 13 // Usage: go test -run=TestCalibrate -calibrate 14 15 package big 16 17 import ( 18 "flag" 19 "fmt" 20 "testing" 21 "time" 22 ) 23 24 var calibrate = flag.Bool("calibrate", false, "run calibration test") 25 26 func karatsubaLoad(b *testing.B) { 27 BenchmarkMul(b) 28 } 29 30 // measureKaratsuba returns the time to run a Karatsuba-relevant benchmark 31 // given Karatsuba threshold th. 32 func measureKaratsuba(th int) time.Duration { 33 th, karatsubaThreshold = karatsubaThreshold, th 34 res := testing.Benchmark(karatsubaLoad) 35 karatsubaThreshold = th 36 return time.Duration(res.NsPerOp()) 37 } 38 39 func computeThresholds() { 40 fmt.Printf("Multiplication times for varying Karatsuba thresholds\n") 41 fmt.Printf("(run repeatedly for good results)\n") 42 43 // determine Tk, the work load execution time using basic multiplication 44 Tb := measureKaratsuba(1e9) // th == 1e9 => Karatsuba multiplication disabled 45 fmt.Printf("Tb = %10s\n", Tb) 46 47 // thresholds 48 th := 4 49 th1 := -1 50 th2 := -1 51 52 var deltaOld time.Duration 53 for count := -1; count != 0 && th < 128; count-- { 54 // determine Tk, the work load execution time using Karatsuba multiplication 55 Tk := measureKaratsuba(th) 56 57 // improvement over Tb 58 delta := (Tb - Tk) * 100 / Tb 59 60 fmt.Printf("th = %3d Tk = %10s %4d%%", th, Tk, delta) 61 62 // determine break-even point 63 if Tk < Tb && th1 < 0 { 64 th1 = th 65 fmt.Print(" break-even point") 66 } 67 68 // determine diminishing return 69 if 0 < delta && delta < deltaOld && th2 < 0 { 70 th2 = th 71 fmt.Print(" diminishing return") 72 } 73 deltaOld = delta 74 75 fmt.Println() 76 77 // trigger counter 78 if th1 >= 0 && th2 >= 0 && count < 0 { 79 count = 10 // this many extra measurements after we got both thresholds 80 } 81 82 th++ 83 } 84 } 85 86 func TestCalibrate(t *testing.T) { 87 if *calibrate { 88 computeThresholds() 89 } 90 }