farm-ng-core
stopwatch.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
18 
19 #include <chrono>
20 #include <iostream>
21 #include <map>
22 #include <optional>
23 #include <stdexcept>
24 
25 namespace farm_ng {
26 
27 /// Stopwatch class for creating multiple, optionally concurrent, timers
29  public:
30  /// Returns an instance of the stopwatch timer.
32  // https://stackoverflow.com/a/17712497
33  static StopwatchSingleton Static_S;
34  return Static_S;
35  }
36 
37  /// Start a new, named timer.
38  void start(std::string str) {
39  auto start_time = std::chrono::high_resolution_clock::now();
40 
41  Bucket& b = timers_[str];
42  FARM_ASSERT(!b.maybe_start, "{} is already started", str);
43  b.maybe_start = start_time;
44  }
45 
46  /// Stops the named timer and returns the duration.
47  double stop(std::string str) {
48  auto stop_time = std::chrono::high_resolution_clock::now();
49 
50  auto it = timers_.find(str);
51  FARM_ASSERT(it != timers_.end());
52  Bucket& b = it->second;
53  FARM_ASSERT(b.maybe_start);
54  std::chrono::duration<double> diff = stop_time - *b.maybe_start;
55  b.maybe_start.reset();
56  double d = diff.count();
57  if (d < b.min) {
58  b.min = d;
59  }
60  if (d > b.max) {
61  b.max = d;
62  }
63  b.sum += d;
64  ++b.num;
65  return d;
66  }
67 
68  /// Container for statistics on stopwatch timers.
69  struct StopwatchStats {
70  double mean;
71  double min;
72  double max;
73  int num;
74  };
75 
76  /// Return container of stopwatch timer statistics.
77  std::map<std::string, StopwatchStats> getStats() {
78  std::map<std::string, StopwatchStats> stats_vec;
79  for (auto const& bucket : timers_) {
80  StopwatchStats stats;
81  stats.mean = bucket.second.sum / bucket.second.num;
82  stats.num = bucket.second.num;
83  stats.min = bucket.second.min;
84  stats.max = bucket.second.max;
85  stats_vec.insert({bucket.first, stats});
86  }
87  return stats_vec;
88  }
89 
90  /// Prints statistics of stopwatch timers.
91  void print() {
92  for (auto const& stats : getStats()) {
93  std::cout << stats.first << " mean time: " << stats.second.mean
94  << " min time: " << stats.second.min
95  << " max time: " << stats.second.max
96  << " num: " << stats.second.num << std::endl;
97  }
98  }
99 
100  private:
101  StopwatchSingleton() {}
102  struct Bucket {
103  std::optional<std::chrono::time_point<std::chrono::high_resolution_clock>>
104  maybe_start;
105 
106  size_t num = 0;
107  double sum = 0.0;
109  double max = std::numeric_limits<double>::lowest();
110  };
111 
112  std::map<std::string, Bucket> timers_;
113 };
114 
115 /// Stopwatch for a single, named timer.
116 struct ScopedTimer {
117  /// Default constructor, requires timer name.
118  ScopedTimer(std::string str) : str_(str) {
120  }
121 
122  /// Destructor that returns the timer duration.
124 
125  private:
126  std::string str_;
127 };
128 
129 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
farm_ng::StopwatchSingleton
Stopwatch class for creating multiple, optionally concurrent, timers.
Definition: stopwatch.h:28
farm_ng::ScopedTimer
Stopwatch for a single, named timer.
Definition: stopwatch.h:116
logger.h
farm_ng::StopwatchSingleton::StopwatchStats
Container for statistics on stopwatch timers.
Definition: stopwatch.h:69
farm_ng::StopwatchSingleton::StopwatchStats::max
double max
Definition: stopwatch.h:72
core.event_service.str
str
Definition: event_service.py:547
sophus::min
auto min(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:104
FARM_ASSERT
#define FARM_ASSERT(condition,...)
If condition is false, Print formatted error message and then panic.
Definition: logger.h:264
farm_ng::StopwatchSingleton::StopwatchStats::min
double min
Definition: stopwatch.h:71
farm_ng::StopwatchSingleton::getStats
std::map< std::string, StopwatchStats > getStats()
Return container of stopwatch timer statistics.
Definition: stopwatch.h:77
farm_ng::StopwatchSingleton::StopwatchStats::mean
double mean
Definition: stopwatch.h:70
farm_ng::sum
auto sum(Expected< A > maybe_left, Expected< A > maybe_right) -> Expected< A >
Definition: expected_test.cpp:98
farm_ng::StopwatchSingleton::StopwatchStats::num
int num
Definition: stopwatch.h:73
farm_ng::StopwatchSingleton::start
void start(std::string str)
Start a new, named timer.
Definition: stopwatch.h:38
farm_ng::StopwatchSingleton::stop
double stop(std::string str)
Stops the named timer and returns the duration.
Definition: stopwatch.h:47
sophus::max
auto max(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:114
farm_ng::ScopedTimer::~ScopedTimer
~ScopedTimer()
Destructor that returns the timer duration.
Definition: stopwatch.h:123
farm_ng::StopwatchSingleton::getInstance
static StopwatchSingleton & getInstance()
Returns an instance of the stopwatch timer.
Definition: stopwatch.h:31
farm_ng::ScopedTimer::ScopedTimer
ScopedTimer(std::string str)
Default constructor, requires timer name.
Definition: stopwatch.h:118
farm_ng::StopwatchSingleton::print
void print()
Prints statistics of stopwatch timers.
Definition: stopwatch.h:91