-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs314_timing.hpp
72 lines (66 loc) · 1.25 KB
/
cs314_timing.hpp
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
#pragma once
#include <stdint.h>
#if (defined _WIN32 || defined WIN32)
#include "windows.h"
#pragma comment(lib, "winmm.lib")
#else
#include <time.h>
#include <sys/time.h>
#endif
class cStopWatch
{
public:
cStopWatch()
{
mPeriod = QueryPeriod();
Start();
}
~cStopWatch(){};
void Start()
{
mStartTicks = QueryTimer();
mNowTicks = mStartTicks;
}
double Stop()
{
mNowTicks=QueryTimer();
return Read();
}
double Read() const
{
if(mNowTicks != mStartTicks)
{
return mPeriod*(mNowTicks - mStartTicks);
}
return mPeriod*(QueryTimer() - mStartTicks);
}
inline float Readf() const{
return static_cast<float>(Read());
}
private:
uint64_t QueryTimer() const
{
#if (defined _WIN32 || defined WIN32)
LARGE_INTEGER time_start;
QueryPerformanceCounter(&time_start);
return static_cast<uint64_t>(time_start.QuadPart);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint64_t>(ts.tv_sec*1000000000 + ts.tv_nsec);
#endif
}
double QueryPeriod() const
{
#if (defined _WIN32 || defined WIN32)
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return 1.0/frequency.QuadPart;
#else
return 1e-9;
#endif
}
uint64_t mStartTicks;
uint64_t mNowTicks;
double mPeriod;
};