项目作者: sadlil

项目描述 :
去秒表。谷歌番石榴秒表的翻译与一些额外的功能。
高级语言: Go
项目地址: git://github.com/sadlil/stopwatch.git
创建时间: 2016-01-12T12:13:45Z
项目社区:https://github.com/sadlil/stopwatch

开源协议:MIT License

下载


StopWatch

stopwatch written in go.

Go Translation of google Guava Stopwatch [com.google.common.base.Stopwatch]

As its name implies, this provides a method to conveniently measure
time elapsed between two code points.

Stopwatch implements a simple stopwatch functionality. Features :

  • An alternate time source can be substituted.
  • The value returned can be interpreted as relative to another timestamp provided
    by timeunit.
  • Several possible ways to create a Stopwatch.
  • Start/Stop at any time or Reset.
  • Take an individual Lap time.
  • Stores the list of each Lap.
  • Handy methods for print the times.

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

Install

  1. $ go get github.com/sadlil/stopwatch

How to Use

Create

  1. // create a new stopwatch from the current time and stopwatch is not started
  2. s := stopwatch.New()
  3. // create a new stopwatch from the current time and stopwatch is started
  4. s := stopwatch.NewStarted()
  5. // create a new stopwatch from a provided time and stopwatch is not started
  6. s := stopwatch.NewFrom(time.Time{})
  7. // create a new stopwatch from a provided time and stopwatch is started
  8. s:= stopwatch.NewStartedFrom(time.Time{})

Start/Stop

  1. // start any stopped stopwatch.
  2. s.Start() // if the stopwatch is already started it will return
  3. // the stopwatch instance and an error. Will not affect the stopwatch.
  4. // stop a running stopwatch
  5. s.Stop() // if the stopwatch is already stopped it will return
  6. // the stopwatch instance and an error. Will not affect the stopwatch.
  7. // reset any stopwatch. this usually clears all the data stored.
  8. s.Reset()

Duration

  1. // return elapsed time in provided timeunit
  2. s.Elapsed(timeunit.TimeUnit) // provided timeunit is a indicator to return the
  3. // result in desired unit. Ex : timeunit.MILLISECONDS
  4. // will return the elapsed time in milliseconds.
  5. // return elapsed time in Nanoseconds
  6. s.ElapsedNanos()
  7. // return elapsed time in go time.Time
  8. s.ElapsedTime()
  9. // return elapsed time in go time.Duration
  10. s.ElapsedDuration()
  11. // return elapsed time in string. basically a go duration.String()
  12. s.ElapsedString()

Print

a helper to directly print the elapsed time with ease.

  1. // prints the elapsed time in string
  2. s.PrintString()
  3. // print the elapsed time in provided timeunit
  4. s.Print(timeunit.TimeUnit)

Lap

utilities for work with laps in the stopwatch.

  1. // adds a lap in the stopwatch with a unique key name
  2. s.AddLap(name)
  3. // get the lap time by name, time will converted to desired timeunit
  4. s.GetLap(name, timeunit.TimeUnit)
  5. // get the lap time in string by name
  6. s.GetLapString()
  7. // get the lap time in nanoseconds by name
  8. s.GetLapNanos(name)
  9. // get all laps name with time in go time.Duration
  10. s.GetLaps()
  11. // helper print functions to print the lap directly.
  12. // prints the lap time in desired timeunit
  13. s.PrintLap(name, timeunit.TimeUnit)
  14. // prints the lap time in string
  15. s.PrintLapString(name)
  16. // print all laps time in string
  17. s.PrintAllLapString()
  18. // print all laps time in desired timeunit
  19. s.PrintAllLap(timeunit.TimeUnit)

License

The MIT License (MIT) - see LICENSE for more details