~ubuntu-branches/ubuntu/trusty/golang/trusty

« back to all changes in this revision

Viewing changes to src/pkg/math/big/calibrate_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
var calibrate = flag.Bool("calibrate", false, "run calibration test")
23
23
 
24
 
// measure returns the time to run f
25
 
func measure(f func()) time.Duration {
26
 
        const N = 100
27
 
        start := time.Now()
28
 
        for i := N; i > 0; i-- {
29
 
                f()
30
 
        }
31
 
        stop := time.Now()
32
 
        return stop.Sub(start) / N
 
24
func karatsubaLoad(b *testing.B) {
 
25
        BenchmarkMul(b)
 
26
}
 
27
 
 
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())
33
35
}
34
36
 
35
37
func computeThresholds() {
37
39
        fmt.Printf("(run repeatedly for good results)\n")
38
40
 
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)
43
44
 
44
45
        // thresholds
45
 
        n := 8 // any lower values for the threshold lead to very slow multiplies
 
46
        th := 4
46
47
        th1 := -1
47
48
        th2 := -1
48
49
 
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)
54
54
 
55
55
                // improvement over Tb
56
56
                delta := (Tb - Tk) * 100 / Tb
57
57
 
58
 
                fmt.Printf("n = %3d  Tk = %8dns  %4d%%", n, Tk, delta)
 
58
                fmt.Printf("th = %3d  Tk = %10s  %4d%%", th, Tk, delta)
59
59
 
60
60
                // determine break-even point
61
61
                if Tk < Tb && th1 < 0 {
62
 
                        th1 = n
 
62
                        th1 = th
63
63
                        fmt.Print("  break-even point")
64
64
                }
65
65
 
66
66
                // determine diminishing return
67
67
                if 0 < delta && delta < deltaOld && th2 < 0 {
68
 
                        th2 = n
 
68
                        th2 = th
69
69
                        fmt.Print("  diminishing return")
70
70
                }
71
71
                deltaOld = delta
74
74
 
75
75
                // trigger counter
76
76
                if th1 >= 0 && th2 >= 0 && count < 0 {
77
 
                        count = 20 // this many extra measurements after we got both thresholds
 
77
                        count = 10 // this many extra measurements after we got both thresholds
78
78
                }
79
79
 
80
 
                n++
 
80
                th++
81
81
        }
82
82
}
83
83