]> Cypherpunks repositories - gostls13.git/commitdiff
math/rand: avoid use of math.Pow in tests.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Tue, 3 Jul 2012 22:38:01 +0000 (00:38 +0200)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Tue, 3 Jul 2012 22:38:01 +0000 (00:38 +0200)
The use of math.Pow for mere squaring can be extremely
slow on soft-float ARM. Even on systems with hardware
floating-point, a speedup in test duration is observed.

On amd64
Before: ok      math/rand       2.009s
After:  ok      math/rand       0.340s

Fixes #3740.

R=dave, golang-dev, r, r
CC=golang-dev
https://golang.org/cl/6348061

src/pkg/math/rand/rand_test.go

index bbd44e3f8b10657bdb8e6e4a54f009e14ada5901..4d3abdb606c526a32ab5074be575ca73acf67f21 100644 (file)
@@ -57,16 +57,13 @@ func (this *statsResults) checkSimilarDistribution(expected *statsResults) error
 
 func getStatsResults(samples []float64) *statsResults {
        res := new(statsResults)
-       var sum float64
-       for i := range samples {
-               sum += samples[i]
+       var sum, squaresum float64
+       for _, s := range samples {
+               sum += s
+               squaresum += s * s
        }
        res.mean = sum / float64(len(samples))
-       var devsum float64
-       for i := range samples {
-               devsum += math.Pow(samples[i]-res.mean, 2)
-       }
-       res.stddev = math.Sqrt(devsum / float64(len(samples)))
+       res.stddev = math.Sqrt(squaresum/float64(len(samples)) - res.mean*res.mean)
        return res
 }