I'm trying to use Go package "time" to benchmark a function in my program. Been searching around, and people seem to use time.NanoSeconds() - but I think this function is no longer in the time package?
What I need is an easy way to start and stop a timer, and print out the value.
Use the existing functionality of the testing package inside your *_test.go:
func BenchmarkMyFunc(b *testing.B) {
for i := 0; i < b.N; i++ {
myBecnhmarkedFunction()
}
}
and invoke go test -bench RE
(RE is a regexp to select the benches, like B
as it means *B*
) typically inside the package/tool directory
Or explicitly from any place in your code - use the Benchmark function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With