-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnut_platform_timing.hpp
More file actions
162 lines (144 loc) · 5.11 KB
/
nut_platform_timing.hpp
File metadata and controls
162 lines (144 loc) · 5.11 KB
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
#pragma once
#include "nut_types.hpp"
namespace nut {
namespace platform {
inline void sleep( uint32_t milliseconds )
{
::SleepEx( milliseconds, TRUE );
}
inline int64_t unixTimestamp()
{
constexpr int64_t c_unixTimeStart = 0x019DB1DED53E8000;
constexpr int64_t c_ticksPerSecond = 10000000;
FILETIME ft;
GetSystemTimeAsFileTime( &ft );
LARGE_INTEGER li = { .u { .LowPart = ft.dwLowDateTime, .HighPart = static_cast<LONG>( ft.dwHighDateTime ) } };
return ( li.QuadPart - c_unixTimeStart ) / c_ticksPerSecond;
}
//! \class PerformanceTimer
//! Native performance timer implementation for high-precision timing of tasks.
class PerformanceTimer {
private:
LARGE_INTEGER frequency_ { { 0, 0 } };
LARGE_INTEGER time_ { { 0, 0 } };
public:
PerformanceTimer()
{
if ( !QueryPerformanceFrequency( &frequency_ ) )
NUT_RUNTIME_EXCEPT( "Couldn't query HPC frequency" );
}
void start()
{
QueryPerformanceCounter( &time_ );
}
double stop()
{
LARGE_INTEGER newTime;
QueryPerformanceCounter( &newTime );
auto delta = ( newTime.QuadPart - time_.QuadPart ) * 1000000;
delta /= frequency_.QuadPart;
double ms = (double)delta / 1000.0;
return ms;
}
};
//! \class PerformanceClock
//! Native performance clock implementation for high-precision loop delta timing.
#ifdef NUT_PLATFORM_WINDOWS
class PerformanceClock {
private:
LARGE_INTEGER current_ { { 0, 0 } };
double frequency_ = 0.0;
double inverse_ = 0.0;
public:
PerformanceClock()
{
LARGE_INTEGER frequency;
if ( !QueryPerformanceFrequency( &frequency ) )
NUT_RUNTIME_EXCEPT( "Couldn't query HPC frequency" );
frequency_ = static_cast<double>( frequency.QuadPart );
inverse_ = ( 1000000.0 / frequency_ );
}
//! Call this once before entering whatever loop is going to then keep calling update()
inline void init()
{
QueryPerformanceCounter( ¤t_ );
}
//! Returns the time in seconds since the last call to update()
inline double update()
{
LARGE_INTEGER new_;
QueryPerformanceCounter( &new_ );
auto delta = ( new_.QuadPart - current_.QuadPart );
current_ = new_;
return ( static_cast<double>( delta ) / frequency_ );
}
//! Returns the time in seconds since the last call to update() without "resetting" the counter
inline double peek() const
{
LARGE_INTEGER new_;
QueryPerformanceCounter( &new_ );
auto delta = ( new_.QuadPart - current_.QuadPart );
return ( static_cast<double>( delta ) / frequency_ );
}
//! Returns the time in microseconds since the last call to update() without "resetting" the counter
inline uint64_t peekMicroseconds() const
{
LARGE_INTEGER new_;
QueryPerformanceCounter( &new_ );
auto delta = ( new_.QuadPart - current_.QuadPart );
return static_cast<uint64_t>( delta * inverse_ );
}
//! Returns the time in milliseconds since the last call to update() without "resetting" the counter
inline double peekMilliseconds() const
{
return static_cast<double>( peekMicroseconds() ) * 0.001;
}
};
#elif NUT_PLATFORM_LINUX
# define SEC_TO_NANOSEC( sec ) ( ( sec ) * ( 1000LL * 1000LL * 1000LL ) )
# define NANOSEC_TO_SEC( nanosec ) ( ( nanosec ) / ( 1000LL * 1000LL * 1000LL ) )
class PerformanceClock {
private:
int64_t current_ = 0;
double frequency_ = 0.0;
double inverse_ = 0.0;
public:
PerformanceClock()
{
timespec res_s;
if ( clock_getres( CLOCK_MONOTONIC_RAW, &res_s ) < 0 )
NUT_RUNTIME_EXCEPT( "clock_getres failed" );
int64_t res = SEC_TO_NANOSEC( res_s.tv_sec ) + res_s.tv_nsec;
frequency_ = static_cast<double>( SEC_TO_NANOSEC( 1 ) );
}
//! Call this once before entering whatever loop is going to then keep calling update()
inline void init()
{
timespec val_s;
if ( clock_gettime( CLOCK_MONOTONIC_RAW, &val_s ) < 0 )
NUT_RUNTIME_EXCEPT( "clock_gettime failed" );
current_ = SEC_TO_NANOSEC( val_s.tv_sec ) + val_s.tv_nsec;
}
//! Returns the time in seconds since the last call to update()
inline double update()
{
timespec val_s;
clock_gettime( CLOCK_MONOTONIC_RAW, &val_s );
int64_t new_ = SEC_TO_NANOSEC( val_s.tv_sec ) + val_s.tv_nsec;
auto delta = ( new_ - current_ );
current_ = new_;
return static_cast<double>( delta ) / frequency_;
}
//! Returns the time in seconds since the last call to update() without "resetting" the counter
inline double peek()
{
timespec val_s;
clock_gettime( CLOCK_MONOTONIC_RAW, &val_s );
int64_t new_ = SEC_TO_NANOSEC( val_s.tv_sec ) + val_s.tv_nsec;
auto delta = ( new_ - current_ );
return static_cast<double>( delta ) / frequency_;
}
};
#endif
}
}