-
Notifications
You must be signed in to change notification settings - Fork 3
/
tinytime.h
129 lines (104 loc) · 3.27 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
#if !defined( TINYTIME_H )
float ttTime( );
#define TINYTIME_H
#endif
#ifdef TT_IMPLEMENTATION
#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
// 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;
}
#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_nsec - prev.tv_nsec) * 1.0e-9);
prev = now;
return elapsed;
}
#endif
#endif // TT_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.
*/