golang banchmarks with benchstat

Here I will show how to run a benchmark in go1.24 and compare 2 implementations.

Imagine we have 2 files with 2 implementations we want to compare.

bu_hash_test.go (old implementation)

And

bu_fast_hash_test.go (new impl)

First, we run
go test --count=10 -bench=./bu_hash_test.go -benchmem -test.v -test.paniconexit0 -test.bench . | tee bu_test_res_hash.txt.
Then do the same for the second file:
go test --count=10 -bench=./bu_fast_hash_test.go -benchmem -test.v -test.paniconexit0 -test.bench . | tee bu_test_res_fast_hash.txt

Then we compare results with benchstat: benchstat bu_test_res_hash.txt bu_test_res_fast_hash.txt. (If it’s not installed, you would want to run go install golang.org/x/perf/cmd/benchstat@latest).

As a result, we’ll see something like this:

Similar Posts

LEAVE A COMMENT