October 2015
Beginner to intermediate
400 pages
14h 44m
English
Benchmark Functions
Benchmarking is the practice of measuring the performance of a program
on a fixed workload.
In Go, a benchmark function looks like a test function, but with the
Benchmark prefix and a *testing.B parameter that
provides most
of the same methods as a *testing.T, plus a few extra related to performance
measurement.
It also exposes an integer field N, which specifies the number
of times to perform the operation being measured.
Here’s a benchmark for IsPalindrome that calls it N times in
a loop.
import "testing"
func BenchmarkIsPalindrome(b *testing.B) {
for i := 0; i < b.N; i++ {
IsPalindrome("A man, a plan, a canal: Panama")
}
}
We run it with the command below. Unlike tests, by default ...