-
Notifications
You must be signed in to change notification settings - Fork 1
/
StopWatch.h
56 lines (47 loc) · 1.4 KB
/
StopWatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//---------------------------------------------------------------------------
#ifndef StopWatchH
#define StopWatchH
#include <windows.h>
#include <System.SysUtils.hpp>
class StopWatch {
public:
StopWatch( bool AutoStart = true ) {
if ( AutoStart ) {
Start();
}
}
void Start() {
if ( !running_ ) {
running_ = true;
Win32Check( ::QueryPerformanceFrequency( &frequency_ ) );
Win32Check( ::QueryPerformanceCounter( &startingTime_ ) );
}
}
void Stop() {
Win32Check( ::QueryPerformanceCounter( &endingTime_ ) );
if ( running_ ) {
elapsedMicroseconds_.QuadPart =
endingTime_.QuadPart - startingTime_.QuadPart;
elapsedMicroseconds_.QuadPart *= 1000000;
elapsedMicroseconds_.QuadPart /= frequency_.QuadPart;
running_ = false;
}
}
LARGE_INTEGER GetElapsedMicroseconds() {
Stop();
return elapsedMicroseconds_;
}
/*
long double GetElapsedTime() {
return static_cast<long double>( GetElapsedMicroseconds().QuadPart ) / 1E6;
}
*/
private:
bool running_ {};
LARGE_INTEGER startingTime_ {};
LARGE_INTEGER endingTime_ {};
LARGE_INTEGER elapsedMicroseconds_ {};
LARGE_INTEGER frequency_ { 1 };
};
//---------------------------------------------------------------------------
#endif