Skip to content
tezvyn:
Menu

How do you use ResetTimer, StopTimer, and RunParallel in Go benchmarks?

Source: pkg.go.devMediumHow cards are made

Tests Go benchmark timer hygiene and parallel execution. A strong answer covers b.StopTimer before setup, b.ResetTimer before the loop, and b.RunParallel for CPU-bound scaling. A red flag is resetting without stopping or using parallel benchmarks for I/O.

What's really being asked

Whether you understand that Go benchmarks measure total elapsed time and heap allocations by default, including setup and teardown, and whether you know how to isolate the code under test. It also checks if you understand the difference between sequential and parallel benchmarks and when parallel execution provides meaningful signal versus noise.

The full answer

Four things in order. First, b.StopTimer pauses the benchmark clock and memory allocation counters before expensive setup such as building large fixtures or seeding random state. Second, b.ResetTimer zeros the elapsed time and allocation counters so the measurement starts clean immediately before the loop; this is critical because b.N may be large and setup costs would otherwise dominate. Third, b.StartTimer resumes measurement if you stopped it earlier, though many candidates simply call b.ResetTimer after setup which implicitly starts timing. Fourth, b.RunParallel is appropriate only for CPU-bound or lock-contention workloads where you want to see how the function scales with GOMAXPROCS; it spins up goroutines that each call the body function and uses sync.Pool or local variables to avoid false sharing.

The mistakes people make

Calling b.ResetTimer without b.StopTimer first, which still counts setup time if it occurs before the reset. Using b.RunParallel for I/O-bound or network-bound functions, which creates goroutine thrashing and measures scheduler latency rather than code efficiency. Forgetting that b.ResetTimer also resets allocation counters, leading to misleading allocs-per-op numbers. Another red flag is manually spawning goroutines inside a normal benchmark instead of using b.RunParallel, because the benchmark runner cannot correctly attribute work or adjust b.N.

What usually comes next

How does b.RunParallel differ from manually calling runtime.GOMAXPROCS? What happens to allocation metrics if you only call b.StopTimer but never reset? How would you benchmark a function that mutates global state and needs isolation between iterations? When should you prefer b.Loop over manual b.N loops in newer Go versions?

A concrete example

Suppose you are benchmarking a JSON serializer. Before the loop you allocate a large nested struct. You call b.StopTimer, build the struct, then call b.ResetTimer. Inside the loop you only call json.Marshal. If the serializer uses a sync.Pool for buffers, you use b.RunParallel to measure whether pool contention rises under concurrency; the body function receives a pointer to testing.PB and calls pb.Next() in its loop. You would not use RunParallel if the benchmark writes to a single bytes.Buffer because the mutex would dominate and hide the serializer's real cost.

Interview question

When benchmarking a function that requires expensive fixture setup, which sequence correctly isolates the measured loop?

  • a.Call b.StopTimer before setup, then b.StartTimer before the loop
  • b.Call b.ResetTimer after setup without first calling b.StopTimer
  • c.Call b.ResetTimer before setup, then b.StartTimer before the loop
  • d.Call b.StopTimer before setup, then b.ResetTimer before the loopCorrect
Why?

StopTimer pauses the clock and allocation counters during setup, while ResetTimer zeros them immediately before the loop so prior costs are excluded. Calling ResetTimer without StopTimer first still counts setup time that occurs before the reset, and merely stopping and starting does not zero prior counters.

Just read this? Test yourself on what you have been reading.

Read the original → pkg.go.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.

See open roles