Go

How do you run benchmarks rigorously and compare two implementations? Explain -benchmem, ReportAllocs, and benchstat.

Question 377MediumGo 1.22 to 1.25

b.ReportAllocs() (or the -benchmem flag for all benchmarks) adds B/op and allocs/op columns. Allocations are often the best predictor of real-world cost because they drive GC work that a micro-benchmark underrepresents.

go test -run='^
  

 -bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > old.txt…","about":["Go (programming language)","Performance, Profiling & Testing"],"proficiencyLevel":"Intermediate","inLanguage":"en","url":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","mainEntityOfPage":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","author":{"@type":"Person","name":"Aniket Mahakalkar"},"isPartOf":{"@type":"WebSite","name":"Go Interview Questions","url":"https://golang-interview-prep.aaniket.in/"}}
    
    
    
    
  
  
    
-bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > old.txt # ...apply change... go test -run='^ -bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > old.txt…","about":["Go (programming language)","Performance, Profiling & Testing"],"proficiencyLevel":"Intermediate","inLanguage":"en","url":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","mainEntityOfPage":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","author":{"@type":"Person","name":"Aniket Mahakalkar"},"isPartOf":{"@type":"WebSite","name":"Go Interview Questions","url":"https://golang-interview-prep.aaniket.in/"}}
-bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > new.txt benchstat old.txt new.txt # golang.org/x/perf/cmd/benchstat

benchstat reports median, confidence interval, and a p-value; "~" means no statistically significant difference. Rigor checklist:

  • -run='^ -bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > old.txt…","about":["Go (programming language)","Performance, Profiling & Testing"],"proficiencyLevel":"Intermediate","inLanguage":"en","url":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","mainEntityOfPage":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","author":{"@type":"Person","name":"Aniket Mahakalkar"},"isPartOf":{"@type":"WebSite","name":"Go Interview Questions","url":"https://golang-interview-prep.aaniket.in/"}}
    to skip unit tests.
  • -count ≥ 6–10 for statistics; interleave runs if the machine is noisy.
  • Quiet machine: no browser, fixed CPU frequency, laptop plugged in.
  • -benchtime=2s or -benchtime=1000x for fixed iterations.
  • Use b.ReportMetric(v, "unit") for custom metrics like MB/s or b.SetBytes(n) for throughput.
  • Sub-benchmarks with b.Run across input sizes to reveal complexity, not just constants.
func BenchmarkEncode(b *testing.B) {
    for _, n := range []int{16, 1024, 64 << 10} {
        b.Run(fmt.Sprintf("size=%d", n), func(b *testing.B) {
            data := make([]byte, n)
            b.SetBytes(int64(n))
            b.ReportAllocs()
            for b.Loop() {
                encode(data)
            }
        })
    }
}

More on Performance, Profiling & Testing

All 38 Performance, Profiling & Testing questions
-bench=Encode -benchmem -count=10 -cpu=1,4 ./codec > old.txt…","about":["Go (programming language)","Performance, Profiling & Testing"],"proficiencyLevel":"Intermediate","inLanguage":"en","url":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","mainEntityOfPage":"https://golang-interview-prep.aaniket.in/questions/how-do-you-run-benchmarks-rigorously-and-compare-two-implementations-explain-ben","author":{"@type":"Person","name":"Aniket Mahakalkar"},"isPartOf":{"@type":"WebSite","name":"Go Interview Questions","url":"https://golang-interview-prep.aaniket.in/"}}