autogenu-jupyter
An automatic code generator and the continuation/GMRES (C/GMRES) based numerical solvers for nonlinear MPC
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1#ifndef CGMRES__TIMER_HPP_
2#define CGMRES__TIMER_HPP_
3
4#include <chrono>
5#include <iostream>
6
7#include "cgmres/types.hpp"
8
9
10namespace cgmres {
11
21
26
30 unsigned long counts = 0;
31
32 void disp(std::ostream& os) const {
33 os << "TimingProfile: " << std::endl;
34 os << " average time: " << average_time_ms << " [ms]" << std::endl;
35 os << " max time: " << max_time_ms << " [ms]" << std::endl;
36 os << " counts: " << counts << std::endl;
37 }
38
39 friend std::ostream& operator<<(std::ostream& os, const TimingProfile& profile) {
40 profile.disp(os);
41 return os;
42 }
43};
44
45
50class Timer {
51public:
55 Timer() { reset(); }
56
60 ~Timer() = default;
61
62 void reset() {
63 counts_ = 0;
64 total_elapsed_time_ = 0;
65 max_elapsed_time_ = 0;
66 }
67
71 void tick() {
72 time_point_ = std::chrono::high_resolution_clock::now();
73 }
74
78 void tock() {
79 const auto now = std::chrono::high_resolution_clock::now();
80 const std::chrono::duration<Scalar, std::milli> elapsed_time = now - time_point_;
81 total_elapsed_time_ += elapsed_time.count();
82 max_elapsed_time_ = std::max(max_elapsed_time_, elapsed_time.count());
83 ++counts_;
84 }
85
91 TimingProfile profile;
92 if (counts_ > 0) {
93 profile.average_time_ms = total_elapsed_time_ / counts_;
94 profile.max_time_ms = max_elapsed_time_;
95 profile.counts = counts_;
96 }
97 return profile;
98 }
99
100private:
101 unsigned long counts_;
102 Scalar total_elapsed_time_, max_elapsed_time_;
103 std::chrono::high_resolution_clock::time_point time_point_;
104};
105
106} // namespace cgmres
107
108#endif // CGMRES__TIMER_HPP_
A timer for benchmarks.
Definition: timer.hpp:50
void reset()
Definition: timer.hpp:62
void tick()
Start timer (tick).
Definition: timer.hpp:71
Timer()
Default constructor.
Definition: timer.hpp:55
TimingProfile getProfile() const
Get timing result as TimingProfile.
Definition: timer.hpp:90
~Timer()=default
Default destructor.
void tock()
Stop the timer (tock).
Definition: timer.hpp:78
Definition: continuation_gmres.hpp:11
double Scalar
Alias of double.
Definition: types.hpp:11
A profile of the timing benchmark.
Definition: timer.hpp:16
Scalar max_time_ms
Maximum computational time in milliseconds.
Definition: timer.hpp:25
void disp(std::ostream &os) const
Definition: timer.hpp:32
Scalar average_time_ms
Average computational time in milliseconds.
Definition: timer.hpp:20
unsigned long counts
Number of timing counts.
Definition: timer.hpp:30
friend std::ostream & operator<<(std::ostream &os, const TimingProfile &profile)
Definition: timer.hpp:39