SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
HighResTimer.h
Go to the documentation of this file.
1 /**
2  * \file Utils/lib-utils/source/HighResTimer.h
3  * \authors Marcus Hudritsch
4  * \details High Resolution Timer that is able to measure the elapsed time
5  * with 1 micro-second accuracy with C++11 high_resolution_clock
6  * \date July 2014
7  * \copyright http://opensource.org/licenses/GPL-3.0
8  * \remarks Please use clangformat to format the code. See more code style on
9  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
10 */
11 
12 #ifndef HIGHRESTIMER
13 #define HIGHRESTIMER
14 
15 #include <chrono>
16 #include <functional>
17 #include <thread>
18 
19 using namespace std;
20 using namespace std::chrono;
21 
22 typedef std::chrono::high_resolution_clock HighResClock;
23 typedef std::chrono::high_resolution_clock::time_point HighResTimePoint;
24 
25 //! High Resolution Timer class using C++11
26 /*!
27 High Resolution Timer that is able to measure the elapsed time with 1
28 micro-second accuracy.
29 */
31 {
32 public:
33  HighResTimer() { _timePoint1 = HighResClock::now(); }
34 
35  void start() { _timePoint1 = HighResClock::now(); }
36  void stop() { _timePoint2 = HighResClock::now(); }
37  float elapsedTimeInSec() { return (float)((double)elapsedTimeInMicroSec() / 1000000.0); }
38  float elapsedTimeInMilliSec() { return (float)((double)elapsedTimeInMicroSec() / 1000.0); }
39  int64_t elapsedTimeInMicroSec() { return duration_cast<microseconds>(HighResClock::now() - _timePoint1).count(); }
40 
41  static void callAfterSleep(int milliSec, const function<void(void)>& callbackFunc)
42  {
43  // Create a thread that immediately sleeps the milliseconds
44  thread t([=]()
45  {
46  this_thread::sleep_for(chrono::milliseconds(milliSec));
47  callbackFunc(); });
48 
49  // detach the thread so that it can exist after the block
50  t.detach();
51  }
52 
53 private:
54  HighResTimePoint _timePoint1; //!< high precision start time point
55  HighResTimePoint _timePoint2; //!< high precision end time point
56 };
57 //---------------------------------------------------------------------------
58 #endif
std::chrono::high_resolution_clock::time_point HighResTimePoint
Definition: HighResTimer.h:23
std::chrono::high_resolution_clock HighResClock
Definition: HighResTimer.h:22
High Resolution Timer class using C++11.
Definition: HighResTimer.h:31
float elapsedTimeInMilliSec()
Definition: HighResTimer.h:38
void start()
Definition: HighResTimer.h:35
float elapsedTimeInSec()
Definition: HighResTimer.h:37
static void callAfterSleep(int milliSec, const function< void(void)> &callbackFunc)
Definition: HighResTimer.h:41
int64_t elapsedTimeInMicroSec()
Definition: HighResTimer.h:39
HighResTimePoint _timePoint1
high precision start time point
Definition: HighResTimer.h:54
HighResTimePoint _timePoint2
high precision end time point
Definition: HighResTimer.h:55