Git Product home page Git Product logo

stopwatch's Introduction

Archived project. No maintenance.

This project is not maintained anymore and is archived. Feel free to fork and make your own changes if needed. For more detail read my blog post: Taking an indefinite sabbatical from my projects

Thanks to everyone for their valuable feedback and contributions.

Stopwatch GoDoc Build Status

Stopwatch implements a simple stopwatch functionality. Features :

  • Two possible ways to create a Stopwatch. Initialized or uninitialized.
  • Start/Stop at any time or Reset.
  • Take an individual Lap time
  • Stores the list of each Lap
  • Satisfies JSON Marshaler/Unmarshaler interface
  • Handy methods like Print()/Log() to log a function execution time with one step.

Feel free to fork and send a pull request for any changes/improvements. For usage see examples below or click on the godoc badge.

Install

go get github.com/fatih/stopwatch

Examples

Basics

// create a new stopwatch, the timer starts immediately.
s := stopwatch.Start(0)

// get elapsed duration at any time
duration := s.ElapsedTime()
// some work ... another elapsed time
duration2 := s.ElapsedTime()

// create a new stopwatch, but do not start immediately
s := stopwatch.New()
// ... start it later
s.Start()

// start a stopwatch after a certain time
s := stopwatch.Start(2 * time.Second)
d1 := s.ElapsedTime() // d1 is zero here
// after two seconds it works
d2 := s.ElapsedTime()

Resume/Stop

// reset the stopwatch
s.Reset()
// .. or stop the stopwatch
s.Stop()

// resume the timer after a reset/stop
s.Start()

Lap

// create a lap
lap1 := s.Lap()
lap2 := s.Lap()
lap3 := s.Lap()

// get a list of all lap durations
list := s.Laps()

// lap returns zero duration if the timer is stopped/reseted
s.Stop()
lap4 := s.Lap() // lap4 == time.Duration(0)

Helpers

// String representation of stopwatch
fmt.Printf("stopwatch: %s", s)

// find out how long a function lasts
// outputs when the function returns:  myFunction - elapsed: 2.000629842s
defer Start(0).Print("myfunction")

// Marshal to a JSON object.
type API struct {
    Name      string     `json:"name"`
    Stopwatch *Stopwatch `json:"elapsed"`
}

a := API{
    Name:      "Example API Call",
    Stopwatch: Start(0),
}

// do some work ...
time.Sleep(time.Millisecond * 20)

b, err := json.Marshal(a)
if err != nil {
    t.Errorf("error: %s\n", err)
}

// output: {"name":"Example API Call","elapsed":"21.351657ms"}
fmt.Println(string(b))

// Unmarshal from a JSON object.
v := new(API)
err = json.Unmarshal(b, v)
if err != nil {
    t.Errorf("error: %s\n", err)
}

// Get back our elapsed time
duration := v.Stopwatch.ElapsedTime()

Credits

License

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

stopwatch's People

Contributors

fatih avatar

Watchers

DeanLJ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.