2014-07-31 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / testing / allocs.go
blob9ec47bd4602ec8a54b9a495649bfc035d44bd449
1 // Copyright 2013 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 package testing
7 import (
8 "runtime"
11 // AllocsPerRun returns the average number of allocations during calls to f.
12 // Although the return value has type float64, it will always be an integral value.
14 // To compute the number of allocations, the function will first be run once as
15 // a warm-up. The average number of allocations over the specified number of
16 // runs will then be measured and returned.
18 // AllocsPerRun sets GOMAXPROCS to 1 during its measurement and will restore
19 // it before returning.
20 func AllocsPerRun(runs int, f func()) (avg float64) {
21 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
23 // Warm up the function
24 f()
26 // Measure the starting statistics
27 var memstats runtime.MemStats
28 runtime.ReadMemStats(&memstats)
29 mallocs := 0 - memstats.Mallocs
31 // Run the function the specified number of times
32 for i := 0; i < runs; i++ {
33 f()
36 // Read the final statistics
37 runtime.ReadMemStats(&memstats)
38 mallocs += memstats.Mallocs
40 // Average the mallocs over the runs (not counting the warm-up).
41 // We are forced to return a float64 because the API is silly, but do
42 // the division as integers so we can ask if AllocsPerRun()==1
43 // instead of AllocsPerRun()<2.
44 return float64(mallocs / uint64(runs))