forked from microsoft/Multiverso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop_watch.cpp
More file actions
executable file
·79 lines (67 loc) · 1.53 KB
/
stop_watch.cpp
File metadata and controls
executable file
·79 lines (67 loc) · 1.53 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#endif
#include <ctime>
#include "stop_watch.h"
namespace multiverso
{
StopWatch::StopWatch()
{
tick_per_sec_ = GetTickPerSec();
Restart();
}
StopWatch::~StopWatch() {}
void StopWatch::Restart()
{
elapsed_tick_ = 0;
start_tick_ = -1;
Start();
}
void StopWatch::Start()
{
if (start_tick_ < 0)
{
start_tick_ = GetCurrentTick();
}
}
void StopWatch::Stop()
{
if (start_tick_ >= 0)
{
elapsed_tick_ += GetCurrentTick() - start_tick_;
start_tick_ = -1;
}
}
bool StopWatch::IsRunning()
{
return start_tick_ >= 0;
}
double StopWatch::ElapsedSeconds()
{
int64_t addition = IsRunning() ? (GetCurrentTick() - start_tick_) : 0;
return static_cast<double>(elapsed_tick_ + addition) / tick_per_sec_;
}
#if defined(_WIN32) || defined(_WIN64)
int64_t StopWatch::GetTickPerSec()
{
LARGE_INTEGER tmp;
QueryPerformanceFrequency(&tmp);
return tmp.QuadPart;
}
int64_t StopWatch::GetCurrentTick()
{
LARGE_INTEGER tmp;
QueryPerformanceCounter(&tmp);
return tmp.QuadPart;
}
#else
int64_t StopWatch::GetTickPerSec()
{
return CLOCKS_PER_SEC;
}
int64_t StopWatch::GetCurrentTick()
{
return clock();
}
#endif
}