DEV Community

Ankit malik
Ankit malik

Posted on

4 1 2 1 1

Golang - How to do Benchmarking

Performance matters — especially when dealing with large-scale systems or performance-critical code. Go (Golang) provides first-class support for benchmarking via the testing package. This article walks you through the fundamentals of benchmarking in Go: what it is, why it's important, and how to run benchmarks to compare multiple functions.


🧠 What is Benchmarking?

Benchmarking is the process of measuring how efficiently a piece of code executes. In Go, this typically involves determining how long a function takes to run, how many resources it uses, or how well it scales under load.

Benchmarking is different from testing — it doesn’t check if your code is correct but it checks how well it performs.


✅ Why We Should Do Benchmarking

Benchmarking is useful when you want to:

  • Compare different implementations of the same logic (e.g., using + vs strings.Builder for string concatenation).
  • Find performance bottlenecks in your application.
  • Track regressions over time, especially in performance-sensitive systems.
  • Make data-driven decisions when optimizing code.

In short, benchmarking gives you quantitative insights into your code’s behavior under stress.


💻 Example

Let’s take an easy and real world example which we often overlook

package benchmark

import (
    "strings"
    "testing"
)

func concatWithPlus(a, b string) string {
    return a + b
}

func concatWithBuilder(a, b string) string {
    var sb strings.Builder
    sb.WriteString(a)
    sb.WriteString(b)
    return sb.String()
}

func BenchmarkConcatWithPlus(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = concatWithPlus("Hello", "World")
    }
}

func BenchmarkConcatWithBuilder(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = concatWithBuilder("Hello", "World")
    }
}
Enter fullscreen mode Exit fullscreen mode

▶️ Running the Benchmark

Save the file as concat_test.go and run:

go test -bench=.
Enter fullscreen mode Exit fullscreen mode

📤 Output

Example output might look like:

goos: linux
goarch: amd64
BenchmarkConcatWithPlus-8        10000000               150 ns/op
BenchmarkConcatWithBuilder-8     20000000                90 ns/op
PASS
ok      example.com/benchmark   2.345s

Enter fullscreen mode Exit fullscreen mode

📖 How to Read the Output

Each line shows:

Field Meaning
BenchmarkConcatWithPlus-8 Benchmark function run on 8 logical CPUs
10000000 Number of iterations Go used for benchmarking
150 ns/op Average time taken for one operation (in nanoseconds)

In this case:

  • concatWithBuilder is faster (90 ns/op) than concatWithPlus (150 ns/op).
  • Therefore, strings.Builder is a more efficient way to concatenate strings in this context.

Optional Memory Metrics

To view memory allocations and usage:

go test -bench=. -benchmem
Enter fullscreen mode Exit fullscreen mode

Sample output:


BenchmarkConcatWithPlus-8        10000000               150 ns/op            16 B/op          1 allocs/op
BenchmarkConcatWithBuilder-8     20000000                90 ns/op             0 B/op          0 allocs/op
Enter fullscreen mode Exit fullscreen mode
Field Meaning
150 ns/op How many bytes of memory were allocated per iteration.
1 allocs/op How many memory allocation events occurred per iteration.

This shows:

  • concatWithPlus allocates memory (16 bytes per operation).
  • concatWithBuilder avoids allocations — a big win for performance.

🏁 Conclusion

Benchmarking in Go is easy to set up and incredibly valuable. It helps you:

  • Understand the performance characteristics of your code
  • Choose the most efficient implementations
  • Catch regressions before they ship to production

By comparing two simple functions, we saw how small differences in implementation can lead to measurable performance improvements.

Redis is #1 most used for AI agent data storage and NoSQL databases. Here\

Redis is #1 most used for AI agent data storage and NoSQL databases. Here's why.

49,000 responses in the Stack Overflow 2025 Developer Survey have spoken: Redis 8 is 2x faster, with Redis Query Engine and vector sets enabling real-time agent memory and semantic caching.

Learn more

Top comments (0)

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash

Signup Now

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay