-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimer.cpp
More file actions
97 lines (77 loc) · 1.47 KB
/
Timer.cpp
File metadata and controls
97 lines (77 loc) · 1.47 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Timer.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <time.h>
#endif
CPerfCounter::CPerfCounter() : _clocks(0), _start(0)
{
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER *)&_freq);
#else
_freq = 1000;
#endif
}
CPerfCounter::~CPerfCounter()
{
// EMPTY!
}
void
CPerfCounter::Start(void)
{
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&_start);
#else
struct timespec s;
clock_gettime( CLOCK_REALTIME, &s );
_start = (i64)s.tv_sec * 1e9 + (i64)s.tv_nsec;
#endif
}
void
CPerfCounter::Stop(void)
{
i64 n;
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&n);
#else
struct timespec s;
clock_gettime( CLOCK_REALTIME, &s );
n = (i64)s.tv_sec * 1e9 + (i64)s.tv_nsec;
#endif
n -= _start;
_start = 0;
_clocks += n;
}
void
CPerfCounter::Reset(void)
{
_clocks = 0;
}
double
CPerfCounter::GetTotalTime(void)
{
//returns millisecond as unit -- second * 1000
#if _WIN32
return (double) (_clocks * 1000) / (double) _freq;
#else
return (double)(_clocks * 1000) / (double) 1e9;
#endif
}
double
CPerfCounter::GetElapsedTime(void)
{
i64 n;
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER *)&n);
#else
struct timespec s;
clock_gettime( CLOCK_REALTIME, &s );
n = (i64)s.tv_sec * 1e9 + (i64)s.tv_nsec;
#endif
#if _WIN32
return (double)((n - _start) * 1000) / (double) _freq;
#else
return (double)((n - _start) * 1000) / (double) 1e9;
#endif
}