Wrongly defined benchmark functions
You should be very careful when defining benchmark functions because you might define them incorrectly. Look at the Go code of the following benchmark function:
func BenchmarkFiboI(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fibo1(i)
}
}
The BenchmarkFibo()
function has a valid name and the correct signature. The bad news is that this benchmark function is logically wrong and is not going to produce any results. The reason for this is that as the b.N
value grows in the way described earlier, the runtime of the benchmark function also increases because of the for
loop. This fact prevents BenchmarkFiboI()
from converging to a stable number, which prevents the function from completing and, therefore, returning any results. For analogous reasons, the next benchmark function is also wrongly implemented:
func BenchmarkfiboII(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fibo1(b.N)
}
}
On the...