-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
171 lines (141 loc) · 3.77 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
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
/*
* This file is part of AV Orchid.
* Copyright (c) 2013, Dmitri R. Kuvshinov <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* @file Timer.cpp
* @brief Timer.h implementation
* @author Dmitri Kuvshinov
*/
#include <avo/Timer.h>
//
// Windows targets use high-performance timer
#if defined(WIN32)
//
#define WIN32_LEAN_AND_MEAN // NO*** stuff might have been defined too
#include <Windows.h>
namespace
{
struct HPT
{
long long start;
double freq;
HPT()
: freq (1.0), start (1)
{
long long fr = 1;
QueryPerformanceFrequency (reinterpret_cast <LARGE_INTEGER*> (&fr));
freq = static_cast <double> (fr);
QueryPerformanceCounter (reinterpret_cast <LARGE_INTEGER*> (&start));
}
double operator()() const
{
long long t = 0;
QueryPerformanceCounter (reinterpret_cast <LARGE_INTEGER*> (&t));
return static_cast <double> (t - start) / freq;
}
} Hpt; // high-performance timer, global static (hidden) object
// should be thread-safe, it's state is not being changed after creation
}
double avo::seconds()
{
return Hpt();
}
double avo::timer_ticks()
{
return Hpt.freq;
}
void avo::sleep(double t)
{
Sleep(static_cast<DWORD>(1000.0 * t));
}
//
// non-Windows Boost-enabled targets may enable use Boost clocks
#elif defined(TIMER_USE_BOOST) // define this macro
// unckecked!
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
double avo::seconds()
{
boost::chrono::duration<double> d = boost::chrono::high_resolution_clock::now().time_since_epoch();
return d.count();
}
double avo::timer_ticks()
{
return 1.0e+9; // pseudoticks, nanoseconds
}
void avo::sleep(double t)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(static_cast<int>(1000.0 * t)));
}
//
// or use Chrono from C++11
#elif _MSC_VER >= 1700 || defined(TIMER_USE_STD_CHRONO) // define this macro
//
#include <chrono>
#include <thread>
double avo::seconds()
{
std::chrono::duration<double> d = std::chrono::high_resolution_clock::now().time_since_epoch();
return d.count();
}
double avo::timer_ticks()
{
return 1.0e+9; // pseudoticks, nanoseconds
}
void avo::sleep(double t)
{
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(1000.0 * t)));
}
//
// if no Boost but we have OpenMP then use OpenMP time things
#elif defined(_OPENMP)
//
#include <omp.h>
double avo::seconds()
{
return omp_get_wtime();
}
double avo::timer_ticks()
{
return omp_get_wticks();
}
// simulation, not CPU-friendly (might use lock to try preemptive thread switching)
void avo::sleep(double t)
{
for (double t1 = seconds() + t; seconds() < t1; )
continue;
}
//
// if neither Windows nor OpenMP is available, then fallback to std C clock
#else
//
#include <ctime>
double avo::seconds()
{
return static_cast<double>(std::clock()) / static_cast<double>(CLOCKS_PER_SEC);
}
double avo::timer_ticks()
{
return CLOCKS_PER_SEC;
}
// simulation, not CPU-friendly
void avo::sleep(double t)
{
const std::clock_t t1 = std::clock() + static_cast<int>(1000.0 * t);
while (std::clock() < t1)
continue;
}
#endif