-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
61 lines (54 loc) · 1.12 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
#include "timer.hpp"
#ifdef _WIN32
#include <Windows.h>
#elif __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#else
#include <time.h>
#include <sys/timeb.h>
#endif
namespace
{
double __secondsPerCycle = 0.0;
#ifdef __MACH__
clock_serv_t __cs;
#endif
}
void system_time::initialize()
{
#ifdef _WIN32
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
__secondsPerCycle = 1.0 / frequency.QuadPart;
#else
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__cs);
__secondsPerCycle = 1e-9;
#endif
}
void system_time::terminate()
{
#ifdef __MACH__
mach_port_deallocate(mach_task_self(), __cs);
#endif
}
uint64_t system_time::get_time()
{
#ifdef _WIN32
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
return (uint64_t)time.QuadPart;
#elif __MACH__
mach_timespec_t ts;
clock_get_time(__cs, &ts);
return (uint64_t)ts.tv_sec * 1000000LL + (uint64_t)ts.tv_nsec / 1000LL;
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000LL + (uint64_t)ts.tv_nsec / 1000LL;
#endif
}
double timer::elapsed_time() const
{
return __secondsPerCycle * (_stop_time - _start_time);
}