Quad-SDK
Loading...
Searching...
No Matches
function_timer.h
1#ifndef FUNCTION_TIMER_H
2#define FUNCTION_TIMER_H
3
4#include <chrono>
5#include <iostream>
6
7namespace quad_utils {
8
11
19 public:
24 FunctionTimer(const char* function_name) {
25 function_name_ = const_cast<char*>(function_name);
26 start_time_ = std::chrono::steady_clock::now();
27 }
28
33 double reportSilent() {
34 stop_time_ = std::chrono::steady_clock::now();
35 std::chrono::duration<double> elapsed =
36 std::chrono::duration_cast<std::chrono::duration<double>>(stop_time_ -
38 double current_time = elapsed.count();
39 return current_time;
40 }
41
46 double current_time = reportSilent();
47 printf("Time spent in %s = %.2es\n", function_name_, current_time);
48 return current_time;
49 }
50
57 double reportStatistics(int n) {
58 double avg_time = reportSilent() / n;
59 printf("Average time spent in %s = %.2es\n", function_name_, avg_time);
60 return avg_time;
61 }
62
68 start_time_ = std::chrono::steady_clock::now();
69 }
70
71 private:
73 std::chrono::time_point<std::chrono::steady_clock> start_time_;
74
76 std::chrono::time_point<std::chrono::steady_clock> stop_time_;
77
80};
81
82} // namespace quad_utils
83
84#endif // FUNCTION_TIMER_H
Definition function_timer.h:18
double reportStatistics()
Report the statistics to the terminal.
Definition function_timer.h:45
std::chrono::time_point< std::chrono::steady_clock > stop_time_
The time at the which the report is queried.
Definition function_timer.h:76
FunctionTimer(const char *function_name)
Constructor for FunctionTimer Class.
Definition function_timer.h:24
void reportAndRestart()
Report the statistics to the terminal and restart the clock.
Definition function_timer.h:66
char * function_name_
Name of the function being timed.
Definition function_timer.h:79
double reportSilent()
Report the statistics without printing to the terminal.
Definition function_timer.h:33
double reportStatistics(int n)
Report the averaged statistics to the terminal over a given number of iterations.
Definition function_timer.h:57
std::chrono::time_point< std::chrono::steady_clock > start_time_
The time at the start of the function call.
Definition function_timer.h:73