-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtimer.cpp
More file actions
101 lines (84 loc) · 2 KB
/
timer.cpp
File metadata and controls
101 lines (84 loc) · 2 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
97
98
99
100
101
#include "timer.h"
#include "trace_perf.h"
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
using namespace std::chrono;
Timer::Timer(bool start)
{
if (start)
restart();
}
void Timer::restart()
{
#ifdef _MSC_VER
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
start_ = li.QuadPart;
#else
start_ = high_resolution_clock::now ();
#endif
}
double Timer::elapsed() const
{
#ifdef _MSC_VER
LARGE_INTEGER li;
static double PCfreq = 1;
for(static bool doOnce=true;doOnce;doOnce=false)
{
QueryPerformanceFrequency(&li);
PCfreq = double(li.QuadPart);
}
QueryPerformanceCounter(&li);
return double(li.QuadPart-start_)/PCfreq;
#else
duration<double> diff = high_resolution_clock::now () - start_;
return diff.count();
#endif
}
double Timer::elapsedAndRestart()
{
#ifdef _MSC_VER
LARGE_INTEGER li;
static double PCfreq = 1;
for(static bool doOnce=true;doOnce;doOnce=false)
{
QueryPerformanceFrequency(&li);
PCfreq = double(li.QuadPart);
}
QueryPerformanceCounter(&li);
__int64 now = li.QuadPart;
double diff = double(now-start_)/PCfreq;
start_ = now;
return diff;
#else
high_resolution_clock::time_point now = high_resolution_clock::now ();
duration<double> diff = now - start_;
start_ = now;
return diff.count ();
#endif
}
void Timer::
test()
{
// It should measure duration with a high accuracy
{
TRACE_PERF("it should measure short intervals as short");
trace_perf_.reset ("it should have a low overhead");
{Timer t;t.elapsed ();}
}
// It should have an overhead less than 1 microsecond
{
TRACE_PERF("it should have a low overhead 10000");
for (int i=0;i<10000;i++) {
Timer t0;
t0.elapsed ();
}
trace_perf_.reset ("it should produce stable measures 10000");
for (int i=0;i<10000;i++) {
Timer t0;
t0.elapsed ();
}
}
}