Helios++
Helios software for LiDAR simulations
TimeWatcher.h
1 #pragma once
2 
3 #include <chrono>
4 #include <iostream>
5 #include <string>
6 #include <memory>
7 
14 public:
15  // *** CONSTRUCTOR *** //
16  // ******************* //
20  TimeWatcher();
21 
22  // *** PUBLIC METHODS *** //
23  // ********************** //
29  void start();
36  void startIfNull();
60  void releaseStart();
65  bool hasStarted() const;
70  void stop();
76  void saveStart();
82  void loadStart();
90  std::shared_ptr<std::chrono::high_resolution_clock::duration>
96  double getElapsedDecimalSeconds();
101  long getElapsedSeconds();
106  long getElapsedMillis();
111  long getElapsedNanos();
116  std::string getElapsedFormat();
122  void reportSeconds(
123  std::string msg = "Total elapsed seconds: "
124  );
125 
131  void reportMillis(
132  std::string msg = "Total elapsed milliseconds: "
133  );
134 
141  void reportFormat(
142  std::string msg = "Total elapsed time: "
143  );
144 
150  void synchronize(TimeWatcher const &source);
151 private:
152  // *** ATTRIBUTES *** //
153  // ******************** //
160  std::unique_ptr<std::chrono::high_resolution_clock::time_point> tStart;
166  std::unique_ptr<std::chrono::high_resolution_clock::time_point> tEnd;
167 
168  // *** CACHE *** //
169  // *************** //
176  std::unique_ptr<std::chrono::high_resolution_clock::time_point> savedStart;
177 
178  // *** PRIVATE METHODS *** //
179  // *********************** //
186  bool hasNulls();
192  void syncTimePoints(
193  std::unique_ptr<std::chrono::high_resolution_clock::time_point>
194  &p,
195  std::unique_ptr<std::chrono::high_resolution_clock::time_point> const
196  &q
197  );
198 };
A time watcher can be used to perform and report time measures.
Definition: TimeWatcher.h:13
std::unique_ptr< std::chrono::high_resolution_clock::time_point > tStart
Start time point.
Definition: TimeWatcher.h:160
void startIfNull()
Start the time watcher but only if it has not been started before. Previous start might come either f...
Definition: TimeWatcher.cpp:22
void saveStart()
Save the start time to local cache.
Definition: TimeWatcher.cpp:40
std::unique_ptr< std::chrono::high_resolution_clock::time_point > savedStart
Used to store a temporary copy of current tStart.
Definition: TimeWatcher.h:176
bool hasNulls()
Check if the time watcher has null start or end time points.
Definition: TimeWatcher.cpp:115
void syncTimePoints(std::unique_ptr< std::chrono::high_resolution_clock::time_point > &p, std::unique_ptr< std::chrono::high_resolution_clock::time_point > const &q)
Synchronize time point with time point .
Definition: TimeWatcher.cpp:119
void loadStart()
Load the start time from local cache.
Definition: TimeWatcher.cpp:43
bool hasStarted() const
Check if time watcher has been started.
Definition: TimeWatcher.cpp:28
void reportFormat(std::string msg="Total elapsed time: ")
Report elapsed time through specified output stream using "HH:MM:SS" format.
Definition: TimeWatcher.cpp:101
void reportSeconds(std::string msg="Total elapsed seconds: ")
Report elapsed seconds through specified output stream.
Definition: TimeWatcher.cpp:89
std::string getElapsedFormat()
Obtain the elapsed time as a string with format "HH:MM:SS".
Definition: TimeWatcher.cpp:76
void synchronize(TimeWatcher const &source)
Synchronizes start, end and saved start times with those of given source.
Definition: TimeWatcher.cpp:107
void stop()
Stop the time watcher, which sets the ending point for a time measure.
Definition: TimeWatcher.cpp:32
long getElapsedNanos()
Obtain the elapsed time as the integer number of nanoseconds.
Definition: TimeWatcher.cpp:69
void releaseStart()
Release stored start so it is null.
Definition: TimeWatcher.cpp:25
void reportMillis(std::string msg="Total elapsed milliseconds: ")
Report elapsed seconds through specified output stream.
Definition: TimeWatcher.cpp:95
long getElapsedMillis()
Obtain the elapsed time as the integer number of milliseconds.
Definition: TimeWatcher.cpp:63
void start()
Start the time watcher, which sets the starting point for a time measure.
Definition: TimeWatcher.cpp:15
std::unique_ptr< std::chrono::high_resolution_clock::time_point > tEnd
End time point.
Definition: TimeWatcher.h:166
TimeWatcher()
Instantiate a time watcher.
Definition: TimeWatcher.cpp:8
long getElapsedSeconds()
Obtain the elapsed time as the integer number of seconds.
Definition: TimeWatcher.cpp:57
double getElapsedDecimalSeconds()
Obtain the elapsed time as the real number of seconds.
Definition: TimeWatcher.cpp:54
std::shared_ptr< std::chrono::high_resolution_clock::duration > getElapsedTime()
Obtain the elapsed time as the difference between the last start() and the last stop() invocations.
Definition: TimeWatcher.cpp:48