Skip to content

testing: Benchmark, impossibly low ns/op #69417

Closed

Description

@cblach

Go version

go version go1.23.1 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/christian.blach/Library/Caches/go-build'
GOENV='/Users/christian.blach/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/christian.blach/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/christian.blach/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.23.1/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.1/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/christian.blach/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/christian.blach/repos/exp/benchmarkbug/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/h9/th1sbhm15pjfb3vx7h5kr46c0000gq/T/go-build1899405527=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I created a simple benchmark file and I get ns/op in the femtosecond range.

package main

import (
	"math/rand/v2"
	"testing"
)

func BenchmarkRandInt(b *testing.B) {
	rand.Int()
}

What did you see happen?

$ go test -bench='.' -count=1
goos: darwin
goarch: arm64
pkg: benchmarkbug
cpu: Apple M3 Pro
BenchmarkRandInt-11 1000000000 0.0000003 ns/op
PASS
ok benchmarkbug 0.210s

What did you expect to see?

I expected not to see operations completed in sub femtosecond time.

Possible cause: It seems that go test believes that it ran 1000000000 tests while it only ran one and then probably divided the run time with 1000000000.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions