-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTimer.cpp
executable file
·127 lines (110 loc) · 2.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Timer.h"
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
typedef DWORD TimerCounterType;
#else
#include <sys/time.h>
#ifdef _POSIX_MONOTONIC_CLOCK
typedef timespec TimerCounterType;
#else
typedef timeval TimerCounterType;
#endif //_POSIX_MONOTONIC_CLOCK
#endif //_WIN32
struct TimerImpl
{
TimerCounterType start;
TimerCounterType current;
};
#ifdef _WIN32
#define GETCURRENTTIME(x) x=timeGetTime()
#else
#ifdef _POSIX_MONOTONIC_CLOCK
#define GETCURRENTTIME(x) clock_gettime(CLOCK_MONOTONIC,&x)
#else
#define GETCURRENTTIME(x) gettimeofday(&x,NULL)
#endif //_POSIX_MONOTONIC_CLOCK
#endif //_WIN32
// Sadly, timersub isn't defined in Solaris. :(
// So we use this instead. (added by Ryan)
#if defined (__SVR4) && defined (__sun)
#include "timersub.h"
#endif
Timer::Timer()
:impl(new TimerImpl)
{
Reset();
}
Timer::~Timer()
{
delete impl;
}
void Timer::Reset()
{
GETCURRENTTIME(impl->start);
impl->current=impl->start;
}
long long Timer::ElapsedTicks()
{
GETCURRENTTIME(impl->current);
return LastElapsedTicks();
}
long long Timer::LastElapsedTicks() const
{
#ifdef _WIN32
return impl->current-impl->start;
#else
#ifdef _POSIX_MONOTONIC_CLOCK
long long ticks = (impl->current.tv_sec-impl->start.tv_sec)*1000 + (impl->current.tv_nsec-impl->start.tv_nsec)/1000000;
return ticks;
#else
timeval delta;
timersub(&impl->current,&impl->start,&delta);
long long ticks = delta.tv_sec*1000 + delta.tv_usec/1000;
return ticks;
#endif //_POSIX_MONOTONIC_CLOCK
#endif //_WIN32
}
double Timer::ElapsedTime()
{
GETCURRENTTIME(impl->current);
return LastElapsedTime();
}
double Timer::LastElapsedTime() const
{
#ifdef _WIN32
return double(impl->current-impl->start)/1000.0;
#else
#ifdef _POSIX_MONOTONIC_CLOCK
double secs=double(impl->current.tv_sec-impl->start.tv_sec);
secs += double(impl->current.tv_nsec-impl->start.tv_nsec)/1000000000.0;
return secs;
#else
timeval delta;
timersub(&impl->current,&impl->start,&delta);
double secs=double(delta.tv_sec);
secs += double(delta.tv_usec)/1000000.0;
return secs;
#endif //_POSIX_MONOTONIC_CLOCK
#endif //_WIN32
}
/*
clock_t Timer::ElapsedTicks()
{
current = clock();
return (current-start);
}
double Timer::ElapsedTime()
{
current = clock();
return double(current-start)/CLOCKS_PER_SEC;
}
clock_t Timer::LastElapsedTicks() const
{
return current-start;
}
double Timer::LastElapsedTime() const
{
return double(current-start)/CLOCKS_PER_SEC;
}
*/