From: Rémy Oudompheng Date: Tue, 3 Jul 2012 22:38:01 +0000 (+0200) Subject: math/rand: avoid use of math.Pow in tests. X-Git-Tag: go1.1rc2~2835 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1a0a09dafecddf2b63befda53aa5e54d74ce19e1;p=gostls13.git math/rand: avoid use of math.Pow in tests. 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 --- diff --git a/src/pkg/math/rand/rand_test.go b/src/pkg/math/rand/rand_test.go index bbd44e3f8b..4d3abdb606 100644 --- a/src/pkg/math/rand/rand_test.go +++ b/src/pkg/math/rand/rand_test.go @@ -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 }