In learning about Go, I see that you can benchmark a function using fmt.Println(testing.Benchmark(BenchmarkFunction))
, having set up the following:
func BenchmarkFunction(b *testing.B) {
n := 42
for i := 0; i < b.N; i++ {
_ = Function(n)
}
}
However, as the BenchmarkFunction
code would be repeated for every benchmark of every method you would do this for (hence a code smell, in terms of DRY), is there a way that this could be rewritten using closures (or in some other manner) such that a benchmark of a function could be rewritten something like:
fmt.println(test.BenchmarkMyFunction(MyFunction(parameters...)))
and added either into my code or the testing
library?