I want to run the benchmarks in my application. Is that possible? If so, how can I? I have been looking around but haven't seen any indication so far.
You can use testing.Benchmark without running go test
.
package main
import (
"fmt"
"testing"
)
func Fib(n int) int {
if n <= 0 {
return 0
} else if n == 1 {
return 1
}
return Fib(n-1) + Fib(n-2)
}
func main() {
res := testing.Benchmark(func(b *testing.B) {
for n := 0; n < b.N; n++ {
Fib(10)
}
})
fmt.Println(res)
// (on my mac)
// 3000000 454 ns/op
}