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
8namespace 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
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
Duration totalExecutionTime
Definition Stopwatch.h:22
friend std::ostream & operator<<(std::ostream &os, const Stats &stats)
Definition Stopwatch.cpp:53
Duration latestExecutionTime
Definition Stopwatch.h:25
Duration averageExecutionTime
Definition Stopwatch.h:24
std::size_t numExecutions
Definition Stopwatch.h:21
const Stats & getStats(const std::string &tag)
Definition Stopwatch.cpp:47
void stop(const std::string &tag)
Definition Stopwatch.cpp:24
void start(const std::string &tag)
Definition Stopwatch.cpp:17
friend std::ostream & operator<<(std::ostream &os, const Stopwatch &stats)
Definition Stopwatch.cpp:64
std::chrono::duration< float > Duration
Definition Stopwatch.h:11
std::chrono::system_clock Clock
Definition Stopwatch.h:10
std::chrono::time_point< Clock > TimePoint
Definition Stopwatch.h:12