-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtimer.cpp
More file actions
64 lines (52 loc) · 1.47 KB
/
Copy pathtimer.cpp
File metadata and controls
64 lines (52 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
#include <timer.h>
Timer::Timer()
{
#ifdef WIN32
QueryPerformanceFrequency(&_frequency);
_start_count.QuadPart = 0;
_end_count.QuadPart = 0;
#else
_start_count.tv_sec = _start_count.tv_usec = 0;
_end_count.tv_sec = _end_count.tv_usec = 0;
#endif
_stopped = 0;
_start_time_microsec = 0;
_end_time_microsec = 0;
}
Timer::~Timer() {}
void Timer::start()
{
_stopped = 0;
#ifdef WIN32
QueryPerformanceCounter(&_start_count);
#else
gettimeofday(&_start_count, nullptr);
#endif
}
void Timer::stop()
{
_stopped = 1;
#ifdef WIN32
QueryPerformanceCounter(&_end_count);
#else
gettimeofday(&_end_count, nullptr);
#endif
}
double Timer::elapsed_time() { return elapsed_time_sec(); }
double Timer::elapsed_time_sec() { return elapsed_time_microsec() * 0.000001; }
double Timer::elapsed_time_milisec() { return elapsed_time_microsec() * 0.001; }
double Timer::elapsed_time_microsec()
{
#ifdef WIN32
if (!_stopped)
QueryPerformanceCounter(&_end_count);
_start_time_microsec = _start_count.QuadPart * (1000000.0 / _frequency.QuadPart);
_end_time_microsec = _end_count.QuadPart * (1000000.0 / _frequency.QuadPart);
#else
if (!_stopped)
gettimeofday(&_end_count, nullptr);
_start_time_microsec = (_start_count.tv_sec * 1000000.0) + _start_count.tv_usec;
_end_time_microsec = (_end_count.tv_sec * 1000000.0) + _end_count.tv_usec;
#endif
return _end_time_microsec - _start_time_microsec;
}