WARPXM v1.10.0
Loading...
Searching...
No Matches
wxtimer.h
Go to the documentation of this file.
1#ifndef wxtimer_h
2#define wxtimer_h
3
4// std includes
5#include <iostream>
6#include <string>
7#include <sstream>
8
9#include <chrono>
10
11#include <mpi.h>
12#include <iomanip>
13
22{
23public:
27 WxTimer() = default;
28
33 {
34 start = clock_.now();
35 }
36
40 void stopTimer()
41 {
42 end = clock_.now();
43 diff = end - start;
44 }
45
51 unsigned daysElapsed() const
52 {
53 return std::chrono::duration_cast<std::chrono::hours>(diff).count() / (24);
54 }
55
62 {
63 return std::chrono::duration_cast<std::chrono::nanoseconds>(diff).count() * 1e-9;
64 }
65
71 std::string timeElapsedAsString() const
72 {
73 std::ostringstream te;
74 te << std::fixed << std::setprecision(3);
75 unsigned days = daysElapsed();
76 real seconds = secondsElapsed();
77
78 if (days > 0)
79 {
80 real remainder_seconds = seconds - days * 24 * 3600;
81 if (days == 1)
82 {
83 te << " 1 day and " << remainder_seconds << " seconds";
84 }
85 else
86 {
87 te << days << " days and " << remainder_seconds << " seconds";
88 }
89 }
90 else
91 {
92 te << seconds << " seconds";
93 }
94 return te.str();
95 }
96
97private:
98 std::chrono::high_resolution_clock clock_;
100 std::chrono::high_resolution_clock::time_point start, end;
101 std::chrono::high_resolution_clock::duration diff;
102};
103
105#endif // wxtimer_h
Timer class which keeps accurate wall time.
Definition: wxtimer.h:22
void startTimer()
Start the timer.
Definition: wxtimer.h:32
void stopTimer()
Stops the timer.
Definition: wxtimer.h:40
WxTimer()=default
Constructor.
std::string timeElapsedAsString() const
Return time elapsed as a string.
Definition: wxtimer.h:71
unsigned daysElapsed() const
Return number of days elapsed.
Definition: wxtimer.h:51
real secondsElapsed() const
Return number of seconds elapsed.
Definition: wxtimer.h:61
#define real
Definition: wmoclunstructuredreconstruction.h:11