-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimer.cpp
77 lines (64 loc) · 1.54 KB
/
Timer.cpp
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
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Timer.h"
#include "Macros.h"
namespace Util {
/*! (static) */
Timer Timer::processTimer;
/*! (static) */
#ifdef _WIN32
bool Timer::initDone = false;
Timer::timer_t Timer::frequency;
#endif
/*! (static) */
double Timer::now() {
return processTimer.getSeconds();
}
// ---------------------------------------
Timer::Timer() :
startTime(0), stopTime(0), running(true) {
#ifdef _WIN32
if(!Timer::initDone) {
if(!QueryPerformanceFrequency(&Timer::frequency)) {
WARN("QueryPerformanceFrequency failed, timer will not work properly!");
}
Timer::initDone = true;
}
#endif
queryTime(&lastReset);
}
void Timer::reset() {
running = true;
queryTime(&lastReset);
startTime = 0;
stopTime = 0;
}
void Timer::stop() {
if (!running) {
return;
}
running = false;
stopTime = getNanosecondsSinceReset();
}
void Timer::resume() {
if (running) {
return;
}
running = true;
startTime += getNanosecondsSinceReset() - stopTime;
}
uint64_t Timer::getNanoseconds() const {
if (running) {
return getNanosecondsSinceReset() - startTime;
} else {
return stopTime - startTime;
}
}
}