去秒表。谷歌番石榴秒表的翻译与一些额外的功能。
As its name implies, this provides a method to conveniently measure
time elapsed between two code points.
Stopwatch implements a simple stopwatch functionality. Features :
Any given Stopwatch instance records elapsed time.
In other words, you can start and stop the stopwatch multiple times
(just don’t start an already started stopwatch and don’t stop an already stopped stopwatch)
stopwatch is to be used to measure independent events.
Feel free to fork.
Any suggestions or pull request will be appreciated
$ go get github.com/sadlil/stopwatch
// create a new stopwatch from the current time and stopwatch is not started
s := stopwatch.New()
// create a new stopwatch from the current time and stopwatch is started
s := stopwatch.NewStarted()
// create a new stopwatch from a provided time and stopwatch is not started
s := stopwatch.NewFrom(time.Time{})
// create a new stopwatch from a provided time and stopwatch is started
s:= stopwatch.NewStartedFrom(time.Time{})
// start any stopped stopwatch.
s.Start() // if the stopwatch is already started it will return
// the stopwatch instance and an error. Will not affect the stopwatch.
// stop a running stopwatch
s.Stop() // if the stopwatch is already stopped it will return
// the stopwatch instance and an error. Will not affect the stopwatch.
// reset any stopwatch. this usually clears all the data stored.
s.Reset()
// return elapsed time in provided timeunit
s.Elapsed(timeunit.TimeUnit) // provided timeunit is a indicator to return the
// result in desired unit. Ex : timeunit.MILLISECONDS
// will return the elapsed time in milliseconds.
// return elapsed time in Nanoseconds
s.ElapsedNanos()
// return elapsed time in go time.Time
s.ElapsedTime()
// return elapsed time in go time.Duration
s.ElapsedDuration()
// return elapsed time in string. basically a go duration.String()
s.ElapsedString()
a helper to directly print the elapsed time with ease.
// prints the elapsed time in string
s.PrintString()
// print the elapsed time in provided timeunit
s.Print(timeunit.TimeUnit)
utilities for work with laps in the stopwatch.
// adds a lap in the stopwatch with a unique key name
s.AddLap(name)
// get the lap time by name, time will converted to desired timeunit
s.GetLap(name, timeunit.TimeUnit)
// get the lap time in string by name
s.GetLapString()
// get the lap time in nanoseconds by name
s.GetLapNanos(name)
// get all laps name with time in go time.Duration
s.GetLaps()
// helper print functions to print the lap directly.
// prints the lap time in desired timeunit
s.PrintLap(name, timeunit.TimeUnit)
// prints the lap time in string
s.PrintLapString(name)
// print all laps time in string
s.PrintAllLapString()
// print all laps time in desired timeunit
s.PrintAllLap(timeunit.TimeUnit)
The MIT License (MIT) - see LICENSE for more details