HerdSoftware  0.4.0
Benchmark.h
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 
7 #ifndef BENCHMARK_H
8 #define BENCHMARK_H
9 
10 using t_clock = std::chrono::high_resolution_clock;
11 
12 class Stopwatch;
13 
14 class Benchmark {
15  friend class Stopwatch;
16 
17 private:
18  struct Measurement {
19  t_clock::duration time;
20  unsigned int nCalls = 0;
21  };
22 
23 public:
24  Stopwatch MakeStopwatch(std::string name);
25  std::unique_ptr<Stopwatch> MakeStopwatchPtr(std::string name);
26 
27  void Print() {
28  std::cout << "Timers results:" << '\n';
29  for (const auto &measurement : m_timers) {
30  std::cout << measurement.first << ": "
31  << std::chrono::duration_cast<std::chrono::milliseconds>(measurement.second.time).count() << " ms ("
32  << std::chrono::duration_cast<std::chrono::milliseconds>(measurement.second.time).count() /
33  measurement.second.nCalls
34  << " per " << measurement.second.nCalls << " calls)" << '\n';
35  }
36  };
37 
38 private:
39  void UpdateTimer(std::string &&timerName, t_clock::duration time) {
40  // Usually we wouldn't do this, but this is actually what we want: operator[] inserts if the key is not in the map,
41  // then we add the duration
42  auto &measurement = m_timers[timerName];
43  measurement.time += time;
44  measurement.nCalls++;
45  };
46 
47  std::unordered_map<std::string, Measurement> m_timers;
48 };
49 
50 class Stopwatch {
51 public:
52  Stopwatch(std::string name, Benchmark *bench) : m_name(std::move(name)), m_bench(bench), m_start(t_clock::now()) {}
53  ~Stopwatch() { m_bench->UpdateTimer(std::move(m_name), t_clock::duration(t_clock::now() - m_start)); }
54 
55 private:
56  std::string m_name;
57 
59 
60  t_clock::time_point m_start;
61 };
62 
63 inline Stopwatch Benchmark::MakeStopwatch(std::string name) { return Stopwatch(std::move(name), this); }
64 inline std::unique_ptr<Stopwatch> Benchmark::MakeStopwatchPtr(std::string name) {
65  return std::make_unique<Stopwatch>(std::move(name), this);
66 }
67 
68 #endif
t_clock
std::chrono::high_resolution_clock t_clock
Definition: Benchmark.h:10
Benchmark
Definition: Benchmark.h:14
Stopwatch::~Stopwatch
~Stopwatch()
Definition: Benchmark.h:53
Stopwatch::m_start
t_clock::time_point m_start
Definition: Benchmark.h:60
Stopwatch::m_name
std::string m_name
Definition: Benchmark.h:56
Stopwatch::Stopwatch
Stopwatch(std::string name, Benchmark *bench)
Definition: Benchmark.h:52
Benchmark::m_timers
std::unordered_map< std::string, Measurement > m_timers
Definition: Benchmark.h:45
Stopwatch
Definition: Benchmark.h:50
Benchmark::Measurement::time
t_clock::duration time
Definition: Benchmark.h:19
Benchmark::MakeStopwatchPtr
std::unique_ptr< Stopwatch > MakeStopwatchPtr(std::string name)
Definition: Benchmark.h:64
Benchmark::Measurement::nCalls
unsigned int nCalls
Definition: Benchmark.h:20
Benchmark::Measurement
Definition: Benchmark.h:18
Benchmark::MakeStopwatch
Stopwatch MakeStopwatch(std::string name)
Definition: Benchmark.h:63
Benchmark::Print
void Print()
Definition: Benchmark.h:27
Benchmark::UpdateTimer
void UpdateTimer(std::string &&timerName, t_clock::duration time)
Definition: Benchmark.h:39
Benchmark::Stopwatch
friend class Stopwatch
Definition: Benchmark.h:15
Stopwatch::m_bench
Benchmark * m_bench
Definition: Benchmark.h:58