Stopwatch.h
Go to the documentation of this file.
1 #ifndef STOPWATCH_H
2 #define STOPWATCH_H
3 
4 #include <chrono>
5 #include <map>
6 #include <ostream>
7 
8 namespace stopwatch
9 {
10  using Clock = std::chrono::system_clock;
11  using Duration = std::chrono::duration<float>; // seconds
12  using TimePoint = std::chrono::time_point<Clock>;
13 
14  class Stats
15  {
16  public:
18  {
19  }
20 
21  std::size_t numExecutions;
23 
26 
27  friend std::ostream& operator<<(std::ostream& os, const Stats& stats);
28  };
29 
30  class Stopwatch
31  {
32 
33  public:
34  Stopwatch();
35 
36  void reset();
37  void start(const std::string& tag);
38  void stop(const std::string& tag);
39 
40  const Stats& getStats(const std::string& tag);
41 
42  friend std::ostream& operator<<(std::ostream& os, const Stopwatch& stats);
43 
44  private:
45  std::map<std::string, Stats> statsMap;
46  std::map<std::string, TimePoint> currentStarts;
47  };
48 
49 } // namespace stopwatch
50 
51 #endif // STOPWATCH_H
stopwatch::TimePoint
std::chrono::time_point< Clock > TimePoint
Definition: Stopwatch.h:12
stopwatch::Stats::Stats
Stats()
Definition: Stopwatch.h:17
stopwatch::Stats::latestExecutionTime
Duration latestExecutionTime
Definition: Stopwatch.h:25
stopwatch::Stats::numExecutions
std::size_t numExecutions
Definition: Stopwatch.h:21
stopwatch::Stats::totalExecutionTime
Duration totalExecutionTime
Definition: Stopwatch.h:22
stopwatch::Stats::operator<<
friend std::ostream & operator<<(std::ostream &os, const Stats &stats)
Definition: Stopwatch.cpp:53
stopwatch::Stopwatch::Stopwatch
Stopwatch()
Definition: Stopwatch.cpp:5
stopwatch::Stopwatch::reset
void reset()
Definition: Stopwatch.cpp:10
stopwatch::Stats
Definition: Stopwatch.h:14
stopwatch
Definition: Stopwatch.cpp:3
stopwatch::Stopwatch::getStats
const Stats & getStats(const std::string &tag)
Definition: Stopwatch.cpp:47
stopwatch::Stopwatch::stop
void stop(const std::string &tag)
Definition: Stopwatch.cpp:24
stopwatch::Stopwatch
Definition: Stopwatch.h:30
stopwatch::Duration
std::chrono::duration< float > Duration
Definition: Stopwatch.h:11
stopwatch::Stats::averageExecutionTime
Duration averageExecutionTime
Definition: Stopwatch.h:24
stopwatch::Stopwatch::start
void start(const std::string &tag)
Definition: Stopwatch.cpp:17
stopwatch::Stopwatch::operator<<
friend std::ostream & operator<<(std::ostream &os, const Stopwatch &stats)
Definition: Stopwatch.cpp:64
stopwatch::Clock
std::chrono::system_clock Clock
Definition: Stopwatch.h:10