Here I have a benchmark test:
// bench_test.go
package main
import (
"testing"
)
func BenchmarkHello(b *testing.B) {
for i := 0; i < b.N; i++ {
a := 1
a++
}
}
The test result are strange. The metric allocs/op
shows zero. I know variable a
is a int type and doesn't take too much memory, but it's not zero.
> go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: a
BenchmarkHello-4 2000000000 0.26 ns/op 0 B/op 0 allocs/op
PASS
ok a 0.553s
Questions
This post points that allocs/op
means how many distinct memory allocations occurred per op (single iteration).
But what does
memory allocation
mean? does that meanset a value for a variable? Is it kind of
malloc in C` ?Why is this metric
allocs/ops
zero?