Skip to content

Instantly share code, notes, and snippets.

@foobit
Created April 1, 2014 19:50
Show Gist options
  • Select an option

  • Save foobit/9921703 to your computer and use it in GitHub Desktop.

Select an option

Save foobit/9921703 to your computer and use it in GitHub Desktop.

Revisions

  1. foobit created this gist Apr 1, 2014.
    9 changes: 9 additions & 0 deletions stopwatch.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class Stopwatch {
    using clock = std::chrono::high_resolution_clock;
    bool is_running() const { return stop_time_ == clock::time_point::min(); }
    clock::time_point end_time() const { return is_running() ? clock::now() : stop_time_; }
    clock::time_point begin_time_{clock::now()}, stop_time_{clock::time_point::min()};
    public:
    void stop() { if (is_running()) stop_time_ = clock::now(); }
    clock::duration elapsed() const { return end_time() - begin_time_; }
    };