mirror of
https://github.com/golang/go
synced 2024-11-19 18:24:39 -07:00
5fea2ccc77
The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package testing
|
|
|
|
import (
|
|
"runtime"
|
|
)
|
|
|
|
// AllocsPerRun returns the average number of allocations during calls to f.
|
|
// Although the return value has type float64, it will always be an integral value.
|
|
//
|
|
// To compute the number of allocations, the function will first be run once as
|
|
// a warm-up. The average number of allocations over the specified number of
|
|
// runs will then be measured and returned.
|
|
//
|
|
// AllocsPerRun sets GOMAXPROCS to 1 during its measurement and will restore
|
|
// it before returning.
|
|
func AllocsPerRun(runs int, f func()) (avg float64) {
|
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
|
|
|
|
// Warm up the function
|
|
f()
|
|
|
|
// Measure the starting statistics
|
|
var memstats runtime.MemStats
|
|
runtime.ReadMemStats(&memstats)
|
|
mallocs := 0 - memstats.Mallocs
|
|
|
|
// Run the function the specified number of times
|
|
for i := 0; i < runs; i++ {
|
|
f()
|
|
}
|
|
|
|
// Read the final statistics
|
|
runtime.ReadMemStats(&memstats)
|
|
mallocs += memstats.Mallocs
|
|
|
|
// Average the mallocs over the runs (not counting the warm-up).
|
|
// We are forced to return a float64 because the API is silly, but do
|
|
// the division as integers so we can ask if AllocsPerRun()==1
|
|
// instead of AllocsPerRun()<2.
|
|
return float64(mallocs / uint64(runs))
|
|
}
|