// a call to StopTimer.
func (b *B) StartTimer() {
if !b.timerOn {
- runtime.ReadMemStats(&memStats)
- b.startAllocs = memStats.Mallocs
- b.startBytes = memStats.TotalAlloc
+ if *benchmarkMemory || b.showAllocResult {
+ runtime.ReadMemStats(&memStats)
+ b.startAllocs = memStats.Mallocs
+ b.startBytes = memStats.TotalAlloc
+ }
b.start = time.Now()
b.timerOn = true
}
func (b *B) StopTimer() {
if b.timerOn {
b.duration += time.Now().Sub(b.start)
- runtime.ReadMemStats(&memStats)
- b.netAllocs += memStats.Mallocs - b.startAllocs
- b.netBytes += memStats.TotalAlloc - b.startBytes
+ if *benchmarkMemory || b.showAllocResult {
+ runtime.ReadMemStats(&memStats)
+ b.netAllocs += memStats.Mallocs - b.startAllocs
+ b.netBytes += memStats.TotalAlloc - b.startBytes
+ }
b.timerOn = false
}
}
// It does not affect whether the timer is running.
func (b *B) ResetTimer() {
if b.timerOn {
- runtime.ReadMemStats(&memStats)
- b.startAllocs = memStats.Mallocs
- b.startBytes = memStats.TotalAlloc
+ if *benchmarkMemory || b.showAllocResult {
+ runtime.ReadMemStats(&memStats)
+ b.startAllocs = memStats.Mallocs
+ b.startBytes = memStats.TotalAlloc
+ }
b.start = time.Now()
}
b.duration = 0
}
// The results of a benchmark run.
+// MemAllocs and MemBytes may be zero if memory benchmarking is not requested
+// using B.ReportAllocs or the -benchmem command line flag.
type BenchmarkResult struct {
N int // The number of iterations.
T time.Duration // The total time taken.
return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds()
}
+// AllocsPerOp returns r.MemAllocs / r.N.
func (r BenchmarkResult) AllocsPerOp() int64 {
if r.N <= 0 {
return 0
return int64(r.MemAllocs) / int64(r.N)
}
+// AllocsPerOp returns r.MemBytes / r.N.
func (r BenchmarkResult) AllocedBytesPerOp() int64 {
if r.N <= 0 {
return 0
return fmt.Sprintf("%8d\t%s%s", r.N, ns, mb)
}
+// MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as 'go test'.
func (r BenchmarkResult) MemString() string {
return fmt.Sprintf("%8d B/op\t%8d allocs/op",
r.AllocedBytesPerOp(), r.AllocsPerOp())