codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

24 / 72 topics
22Error Handling23Testing in Go24Benchmarking25Profiling26Code Organization and Packages
Tutorials/Go (Golang)/Benchmarking
🐹Go (Golang)

Benchmarking

Updated 2026-04-20
3 min read

Benchmarking in Go (Golang)

Benchmarking is a crucial aspect of software development that helps you measure and optimize the performance of your code. In this section, we will explore how to effectively benchmark your Go applications using the built-in testing package. We'll cover everything from writing basic benchmarks to advanced techniques for accurate measurement.

Understanding Benchmarking

Benchmarking involves measuring the execution time of a piece of code under controlled conditions. The goal is to identify performance bottlenecks and optimize them. In Go, benchmarking is integrated into the language's standard library through the testing package, which provides tools for writing and running benchmarks.

Writing Basic Benchmarks

To write a benchmark in Go, you need to define a function with the following signature:

func BenchmarkXxx(b *testing.B) {
    // Code to be benchmarked goes here
}

The Benchmark prefix is mandatory, and the function must take a single argument of type *testing.B. The b.N variable represents the number of times the code inside the loop will be executed. It's automatically adjusted by the testing framework to ensure accurate measurement.

Example: Benchmarking a Function

Let's say we have a simple function that calculates the factorial of a number:

package main

import (
    "fmt"
)

func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}

func main() {
    fmt.Println(factorial(5))
}

To benchmark this function, we can write the following test:

package main

import (
    "testing"
)

func BenchmarkFactorial(b *testing.B) {
    for i := 0; i < b.N; i++ {
        factorial(10)
    }
}

Running Benchmarks

To run benchmarks, use the go test command with the -bench flag:

go test -bench=.

This command will execute all benchmark functions in the package. You can also specify a particular benchmark to run using a regular expression:

go test -bench=BenchmarkFactorial

Advanced Benchmarking Techniques

1. Preventing Optimization

Go's compiler optimizes code aggressively, which can sometimes lead to misleading benchmark results. To prevent this, you can use the testing.B.StopTimer() and testing.B.StartTimer() methods to control when the timer is running.

func BenchmarkFactorialOptimized(b *testing.B) {
    b.StopTimer()
    result := factorial(10)
    b.StartTimer()

    for i := 0; i < b.N; i++ {
        factorial(result)
    }
}

2. Memory Allocation

Benchmarking memory allocation is crucial for understanding the performance characteristics of your code. Go provides the -benchmem flag to report memory allocations:

go test -bench=. -benchmem

This will show you the number of allocations and bytes allocated per operation.

3. Parallel Benchmarks

For CPU-bound tasks, you can run benchmarks in parallel using the b.RunParallel() method:

func BenchmarkFactorialParallel(b *testing.B) {
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            factorial(10)
        }
    })
}

This will distribute the benchmark workload across multiple CPU cores, providing a more accurate measurement of parallel performance.

Best Practices

  • Use Realistic Data: Ensure that your benchmarks use realistic input data to reflect real-world usage.
  • Avoid I/O Operations: I/O operations can skew benchmark results. Use in-memory data or mock objects when possible.
  • Warm-Up the Code: Sometimes, the first execution of a function can be slower due to JIT compilation. Consider adding a warm-up phase before starting the actual benchmark.
  • Compare with Baselines: Always have a baseline benchmark to compare against as you make changes to your code.
  • Profile and Optimize: Use profiling tools like pprof to identify bottlenecks and optimize them.

Conclusion

Benchmarking is an essential tool for optimizing Go applications. By understanding how to write, run, and analyze benchmarks effectively, you can identify performance issues and improve the efficiency of your code. Remember to follow best practices to ensure accurate and meaningful results.

In the next section, we will explore testing strategies and techniques in Go, which complement benchmarking by ensuring your code behaves as expected under various conditions.


PreviousTesting in GoNext Profiling

Recommended Gear

Testing in GoProfiling