forked from krishauser/KrisLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
executable file
·102 lines (89 loc) · 1.92 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
#include "Timer.h"
#include <stdlib.h>
#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()
{
Reset();
}
void Timer::Reset()
{
GETCURRENTTIME(start);
current=start;
}
long long Timer::ElapsedTicks()
{
GETCURRENTTIME(current);
return LastElapsedTicks();
}
long long Timer::LastElapsedTicks() const
{
#ifdef WIN32
return current-start;
#else
#ifdef _POSIX_MONOTONIC_CLOCK
long long ticks = (current.tv_sec-start.tv_sec)*1000 + (current.tv_nsec-start.tv_nsec)/1000000;
return ticks;
#else
timeval delta;
timersub(¤t,&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(current);
return LastElapsedTime();
}
double Timer::LastElapsedTime() const
{
#ifdef WIN32
return double(current-start)/1000.0;
#else
#ifdef _POSIX_MONOTONIC_CLOCK
double secs=double(current.tv_sec-start.tv_sec);
secs += double(current.tv_nsec-start.tv_nsec)/1000000000.0;
return secs;
#else
timeval delta;
timersub(¤t,&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;
}
*/