Skip to content
/ go-bm Public

Super simple tool for measuring run time of your go code

Notifications You must be signed in to change notification settings

Gurenax/go-bm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Go BM (Benchmark)

Super simple tool for measuring run time of your go code

Why Go BM?

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.

Install

go get -u github.com/Gurenax/go-bm

Example

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
}

API

struct BM

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 Start

func (b *BM) Start()

The function populates the startTime value.

func End

func (b *BM) End()

The function populates the endTime value and calculations the duration value.

func DurationInNanoseconds

func (b *BM) DurationInNanoseconds(label string)

The function prints out the duration in Nanoseconds.

func DurationInMicroseconds(label string)

func (b *BM) DurationInMicroseconds(label string)

The function prints out the duration in Microseconds.

func DurationInMilliseconds(label string)

func (b *BM) DurationInMilliseconds(label string)

The function prints out the duration in Milliseconds.

func DurationInSeconds(lanel string)

func (b *BM) DurationInSeconds(label string)

The function prints out the duration in Seconds.

About

Super simple tool for measuring run time of your go code

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages