Super simple tool for measuring run time of your go code
If you need a library that can quickly benchmark your code without the need to deal with the time
library from scratch. It is very simple, yet I find myself using it a lot on my personal projects.
go get -u github.com/Gurenax/go-bm
package main
import (
bm "github.com/Gurenax/go-bm"
)
func main() {
// Starting the Benchmark
b := bm.BM{}
b.Start()
// Your function
result := calculateSomething()
// Ending the Benchmark
b.End()
// Displaying the Duration
b.DurationInNanoseconds("Duration in Nanoseconds:") // Duration in Nanoseconds: 1720.000000ns
b.DurationInMicroseconds("Duration in Microseconds:") // Duration in Microseconds: 1.720000µs
b.DurationInMilliseconds("Duration in Milliseconds:") // Duration in Milliseconds: 0.001720ms
b.DurationInSeconds("Duration in Seconds:") // Duration in Seconds: 0.000002s
}
type BM struct {
startTime time.Time
endTime time.Time
duration time.Duration
}
The struct will contain the start and end time of the benchmark. This needs to be initialised as empty.
e.g. b := bm.BM{}
func (b *BM) Start()
The function populates the startTime
value.
func (b *BM) End()
The function populates the endTime
value and calculations the duration
value.
func (b *BM) DurationInNanoseconds(label string)
The function prints out the duration in Nanoseconds.
func (b *BM) DurationInMicroseconds(label string)
The function prints out the duration in Microseconds.
func (b *BM) DurationInMilliseconds(label string)
The function prints out the duration in Milliseconds.
func (b *BM) DurationInSeconds(label string)
The function prints out the duration in Seconds.