Benchmarks
What is a benchmark?
Section titled “What is a benchmark?”A benchmark measures how long a piece of code takes to run. Go benchmarks live in _test.go files alongside unit tests and are run by the same go test command — just with different flags.
A benchmark function has this signature:
func BenchmarkXxx(b *testing.B) { // ... setup code (not measured) ... for b.Loop() { // Go 1.24+ preferred form // code under measurement }}The testing.B value drives the loop. The framework starts with a small value for b.N and keeps increasing it until the benchmark runs long enough to produce a stable measurement. You never set b.N yourself.
Two loop forms
Section titled “Two loop forms”Go 1.24 introduced b.Loop() as the preferred alternative to the classic b.N counter loop:
// Classic form — works in all Go versionsfunc BenchmarkFibClassic(b *testing.B) { for i := 0; i < b.N; i++ { Fibonacci(20) }}
// Modern form — Go 1.24+func BenchmarkFib(b *testing.B) { for b.Loop() { Fibonacci(20) }}Both forms are correct. b.Loop() is preferred in Go 1.24+ because it handles timer reset internally and is less error-prone.
Setup outside the loop
Section titled “Setup outside the loop”If your benchmark needs expensive setup (opening a file, building a large slice), do it before the loop and call b.ResetTimer() so setup time is not counted:
func BenchmarkIsPrimeList(b *testing.B) { numbers := make([]int, 1000) for i := range numbers { numbers[i] = i + 2 } b.ResetTimer() // start measuring from here
for b.Loop() { for _, n := range numbers { IsPrime(n) } }}Running benchmarks
Section titled “Running benchmarks”Unit tests do not run benchmarks by default. Use the -bench flag:
# Run all benchmarks, show memory allocationsgo test -bench=. -benchmem ./...
# Run only benchmarks matching a name patterngo test -bench=BenchmarkFib -benchmem ./...
# Run benchmarks for at least 5 seconds per functiongo test -bench=. -benchtime=5s ./...Reading the output
Section titled “Reading the output”BenchmarkFib-8 5000000 234 ns/opBenchmarkFibClassic-8 5000000 236 ns/op 0 B/op 0 allocs/op| Column | Meaning |
|---|---|
-8 suffix | GOMAXPROCS value (8 logical CPUs) |
5000000 | Number of iterations b.N ran |
234 ns/op | Nanoseconds per operation |
0 B/op | Bytes allocated per operation (needs -benchmem) |
0 allocs/op | Heap allocations per operation (needs -benchmem) |
Fibonacci allocates nothing because it uses only stack variables — that is why both alloc columns are zero. A function that builds a []string would show non-zero values.