forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinytime.h
209 lines (163 loc) · 5.04 KB
/
tinytime.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
tinytime.h - v1.00
To create implementation (the function definitions)
#define TINYTIME_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
*/
#if !defined( TINYTIME_H )
#include <stdint.h>
typedef struct ttTimer ttTimer;
// quick and dirty elapsed time since last call
float ttTime( );
// high precision timer functions found below
// Call once to setup this timer on this thread
// does a ttRecord call
void ttInitTimer( ttTimer* timer );
// returns raw ticks in platform-specific units between now-time and last ttRecord call
int64_t ttElapsed( ttTimer* timer );
// ticks to seconds conversion
int64_t ttSeconds( ttTimer* timer, int64_t ticks );
// ticks to milliseconds conversion
int64_t ttMilliSeconds( ttTimer* timer, int64_t ticks );
// ticks to microseconds conversion
int64_t ttMicroSeconds( ttTimer* timer, int64_t ticks );
// records the now-time in raw platform-specific units
void ttRecord( ttTimer* timer );
#define TT_WINDOWS 1
#define TT_MAC 2
#define TT_UNIX 3
#if defined( _WIN32 )
#define TT_PLATFORM TT_WINDOWS
#elif defined( __APPLE__ )
#define TT_PLATFORM TT_MAC
#else
#define TT_PLATFORM TT_UNIX
#endif
#if TT_PLATFORM == TT_WINDOWS
struct ttTimer
{
LARGE_INTEGER freq;
LARGE_INTEGER prev;
};
#elif TT_PLATFORM == TT_MAC
#else
#endif
#define TINYTIME_H
#endif
#ifdef TINYTIME_IMPLEMENTATION
// These functions are intended be called from a single thread only. In a
// multi-threaded environment make sure to call Time from the main thread only.
// Also try to set a thread affinity for the main thread to avoid core swaps.
// This can help prevent annoying bios bugs etc. from giving weird time-warp
// effects as the main thread gets passed from one core to another. This shouldn't
// be a problem, but it's a good practice to setup the affinity. Calling these
// functions on multiple threads multiple times will grant a heft performance
// loss in the form of false sharing due to CPU cache synchronization across
// multiple cores.
// More info: https://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx
#if TT_PLATFORM == TT_WINDOWS
float ttTime( )
{
static int first = 1;
static LARGE_INTEGER prev;
static double factor;
LARGE_INTEGER now;
QueryPerformanceCounter( &now );
if ( first )
{
first = 0;
prev = now;
LARGE_INTEGER freq;
QueryPerformanceFrequency( &freq );
factor = 1.0 / (double)freq.QuadPart;
return 0;
}
float elapsed = (float)((double)(now.QuadPart - prev.QuadPart) * factor);
prev = now;
return elapsed;
}
void ttInitTimer( ttTimer* timer )
{
QueryPerformanceCounter( &timer->prev );
QueryPerformanceFrequency( &timer->freq );
}
int64_t ttElapsed( ttTimer* timer )
{
LARGE_INTEGER now;
QueryPerformanceCounter( &now );
return (int64_t)(now.QuadPart - timer->prev.QuadPart);
}
int64_t ttSeconds( ttTimer* timer, int64_t ticks )
{
return ticks / timer->freq.QuadPart;
}
int64_t ttMilliSeconds( ttTimer* timer, int64_t ticks )
{
return ticks / (timer->freq.QuadPart / 1000);
}
int64_t ttMicroSeconds( ttTimer* timer, int64_t ticks )
{
return ticks / (timer->freq.QuadPart / 1000000);
}
void ttRecord( ttTimer* timer )
{
QueryPerformanceCounter( &timer->prev );
}
#elif TT_PLATFORM == TT_MAC
#include <mach/mach_time.h>
float ttTime( )
{
static int first = 1;
static uint64_t prev = 0;
static double factor;
if ( first )
{
first = 0;
mach_timebase_info_data_t info;
mach_timebase_info( &info );
factor = (double)info.numer / ((double)info.denom * 1.0e9);
prev = mach_absolute_time( );
return 0;
}
uint64_t now = mach_absolute_time( );
float elapsed = (float)((double)(now - prev) * factor);
prev = now;
return elapsed;
}
#else
#include <time.h>
float ttTime( )
{
static int first = 1;
struct timespec prev;
if ( first )
{
first = 0;
clock_gettime( CLOCK_MONOTONIC, &prev );
return 0;
}
struct timespec now;
clock_gettime( CLOCK_MONOTONIC, &now );
float elapsed = (float)((double)(now.tv_sec - prev.tv_sec) + ((double)(now.tv_nsec - prev.tv_nsec) * 1.0e-9));
prev = now;
return elapsed;
}
#endif
#endif // TINYTIME_IMPLEMENTATION
/*
zlib license:
Copyright (c) 2016 Randy Gaul http://www.randygaul.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/