Go benchmarks are essential for measuring performance accurately by running code multiple times to report execution time and memory usage, enabling comparison of implementations and detection of regressions. 1. Write benchmark functions with the signature func BenchmarkFunctionName(b *testing.B) in _test.go files, ensuring the function name starts with "Benchmark". 2. Avoid setup overhead by placing initialization code outside the b.N loop and using b.ResetTimer() to exclude setup time from measurements. 3. Use b.ReportAllocs() to track memory allocations and identify inefficiencies in bytes and allocations per operation. 4. Compare multiple implementations by writing separate benchmarks for each version, such as testing string concatenation with = versus strings.Builder. 5. Use sub-benchmarks with b.Run() to test different input sizes and analyze performance scaling. 6. Ensure reliable results by running benchmarks on a quiet machine, using -benchtime for longer runs, -count for multiple trials, and avoiding print statements that skew timing. By following these practices, Go developers can precisely measure and improve code efficiency through data-driven optimization, making benchmarks a critical tool in performance tuning.
When optimizing Go code, benchmarks are essential for measuring performance accurately. Unlike regular tests, benchmarks run your code many times and report how long it takes, helping you compare different implementations and catch regressions early.

Here’s how to write and use Go benchmarks effectively.
What Is a Benchmark in Go?
A benchmark in Go is a function that starts with Benchmark
and follows a specific signature:

func BenchmarkFunctionName(b *testing.B) { ... }
It lives in a _test.go
file alongside your regular tests. The *testing.B
parameter gives you control over the benchmark loop and access to timing data.
For example, say you have a function that concatenates strings:

func ConcatStrings(strings []string) string { var result string for _, s := range strings { result = s } return result }
You can benchmark it like this:
func BenchmarkConcatStrings(b *testing.B) { input := []string{"a", "b", "c", "d", "e"} for i := 0; i < b.N; i { ConcatStrings(input) } }
Run it with:
go test -bench=.
Go will output something like:
BenchmarkConcatStrings-8 1000000 1200 ns/op
This means each operation took about 1200 nanoseconds on average, based on 1 million runs.
Best Practices for Writing Useful Benchmarks
1. Avoid Setup Overhead in the Loop
If you have initialization code (like building input data), do it outside the loop:
func BenchmarkConcatStrings(b *testing.B) { input := make([]string, 1000) for i := range input { input[i] = "x" } b.ResetTimer() // Optional: ignore setup time for i := 0; i < b.N; i { ConcatStrings(input) } }
This ensures only the actual function call is measured.
2. Use b.ReportAllocs()
to Track Memory
Memory allocations can slow down your code. Add:
b.ReportAllocs()
And you’ll see allocation stats:
BenchmarkConcatStrings-8 1000000 1200 ns/op 5000 B/op 5 allocs/op
This shows 5000 bytes and 5 allocations per operation — useful for spotting inefficiencies.
3. Compare Multiple Implementations
Want to see if strings.Builder
is faster? Write a second version and benchmark both:
func ConcatWithBuilder(strings []string) string { var builder strings.Builder for _, s := range strings { builder.WriteString(s) } return builder.String() } func BenchmarkConcatWithBuilder(b *testing.B) { input := []string{"a", "b", "c", "d", "e"} for i := 0; i < b.N; i { ConcatWithBuilder(input) } }
Now run:
go test -bench=.
You’ll likely see the builder version is faster and allocates less.
Subtests and Parameterized Benchmarks
You can use sub-benchmarks to test different input sizes:
func BenchmarkConcatStrings_Size(b *testing.B) { for _, size := range []int{10, 100, 1000} { b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) { input := make([]string, size) for i := range input { input[i] = "x" } for i := 0; i < b.N; i { ConcatStrings(input) } }) } }
Run with:
go test -bench=.
Output will show results for each size:
BenchmarkConcatStrings_Size/Size10-8 5000000 300 ns/op BenchmarkConcatStrings_Size/Size100-8 500000 3500 ns/op BenchmarkConcatStrings_Size/Size1000-8 50000 40000 ns/op
This helps identify how your code scales.
Tips for Reliable Results
- Always run benchmarks on a quiet machine (no heavy background tasks).
- Use
-benchtime
to run longer (e.g.,go test -bench=. -benchtime=5s
) for more stable results. - Use
-count
to run multiple times and check consistency:go test -bench=. -count=3
- Avoid printing in benchmarks — it skews results.
Final Thoughts
Go benchmarks are simple but powerful. With just a few lines, you can:
- Measure execution time
- Track memory usage
- Compare algorithms
- Detect performance regressions
Used consistently, they help you write faster, more efficient code — and prove it.
Basically, if you're tuning performance, don’t guess. Benchmark.
The above is the detailed content of Writing Benchmarks in Go to Measure Performance. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TointegrateGolangserviceswithexistingPythoninfrastructure,useRESTAPIsorgRPCforinter-servicecommunication,allowingGoandPythonappstointeractseamlesslythroughstandardizedprotocols.1.UseRESTAPIs(viaframeworkslikeGininGoandFlaskinPython)orgRPC(withProtoco

Golangofferssuperiorperformance,nativeconcurrencyviagoroutines,andefficientresourceusage,makingitidealforhigh-traffic,low-latencyAPIs;2.Python,whileslowerduetointerpretationandtheGIL,provideseasierdevelopment,arichecosystem,andisbettersuitedforI/O-bo

Golang is mainly used for back-end development, but it can also play an indirect role in the front-end field. Its design goals focus on high-performance, concurrent processing and system-level programming, and are suitable for building back-end applications such as API servers, microservices, distributed systems, database operations and CLI tools. Although Golang is not the mainstream language for web front-end, it can be compiled into JavaScript through GopherJS, run on WebAssembly through TinyGo, or generate HTML pages with a template engine to participate in front-end development. However, modern front-end development still needs to rely on JavaScript/TypeScript and its ecosystem. Therefore, Golang is more suitable for the technology stack selection with high-performance backend as the core.

The key to installing Go is to select the correct version, configure environment variables, and verify the installation. 1. Go to the official website to download the installation package of the corresponding system. Windows uses .msi files, macOS uses .pkg files, Linux uses .tar.gz files and unzip them to /usr/local directory; 2. Configure environment variables, edit ~/.bashrc or ~/.zshrc in Linux/macOS to add PATH and GOPATH, and Windows set PATH to Go in the system properties; 3. Use the government command to verify the installation, and run the test program hello.go to confirm that the compilation and execution are normal. PATH settings and loops throughout the process

Golang usually consumes less CPU and memory than Python when building web services. 1. Golang's goroutine model is efficient in scheduling, has strong concurrent request processing capabilities, and has lower CPU usage; 2. Go is compiled into native code, does not rely on virtual machines during runtime, and has smaller memory usage; 3. Python has greater CPU and memory overhead in concurrent scenarios due to GIL and interpretation execution mechanism; 4. Although Python has high development efficiency and rich ecosystem, it consumes a high resource, which is suitable for scenarios with low concurrency requirements.

To build a GraphQLAPI in Go, it is recommended to use the gqlgen library to improve development efficiency. 1. First select the appropriate library, such as gqlgen, which supports automatic code generation based on schema; 2. Then define GraphQLschema, describe the API structure and query portal, such as defining Post types and query methods; 3. Then initialize the project and generate basic code to implement business logic in resolver; 4. Finally, connect GraphQLhandler to HTTPserver and test the API through the built-in Playground. Notes include field naming specifications, error handling, performance optimization and security settings to ensure project maintenance

The choice of microservice framework should be determined based on project requirements, team technology stack and performance expectations. 1. Given the high performance requirements, KitEx or GoMicro of Go is given priority, especially KitEx is suitable for complex service governance and large-scale systems; 2. FastAPI or Flask of Python is more flexible in rapid development and iteration scenarios, suitable for small teams and MVP projects; 3. The team's skill stack directly affects the selection cost, and if there is already Go accumulation, it will continue to be more efficient. The Python team's rash conversion to Go may affect efficiency; 4. The Go framework is more mature in the service governance ecosystem, suitable for medium and large systems that need to connect with advanced functions in the future; 5. A hybrid architecture can be adopted according to the module, without having to stick to a single language or framework.

sync.WaitGroup is used to wait for a group of goroutines to complete the task. Its core is to work together through three methods: Add, Done, and Wait. 1.Add(n) Set the number of goroutines to wait; 2.Done() is called at the end of each goroutine, and the count is reduced by one; 3.Wait() blocks the main coroutine until all tasks are completed. When using it, please note: Add should be called outside the goroutine, avoid duplicate Wait, and be sure to ensure that Don is called. It is recommended to use it with defer. It is common in concurrent crawling of web pages, batch data processing and other scenarios, and can effectively control the concurrency process.
