I'm playing around with benchmarks in Go and I have a simple function that just sleeps for 5 Nanoseconds, however when I run a benchmark test it shows 298.1 ns/op
. I'm curious why is that. Shouldn't it be 5 ns/op
?
Go version:
go version go1.19 linux/amd64
The code:
package andrei
import (
"testing"
"time"
)
func Hi() {
time.Sleep(5 * time.Nanosecond)
}
func BenchmarkHi(b *testing.B) {
for i := 0; i < b.N; i++ {
Hi()
}
}
The results:
$ go test -run none -bench . -benchmem ./andrei
goos: linux
goarch: amd64
pkg: andrei/andrei
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkHi-8 3861392 298.1 ns/op 0 B/op 0 allocs/op
PASS
ok andrei/andrei 1.470s