22 #include "benchmark/benchmark.h" 30 void BM_Duration_Factory_Nanoseconds(benchmark::State& state) {
32 while (state.KeepRunning()) {
37 BENCHMARK(BM_Duration_Factory_Nanoseconds);
39 void BM_Duration_Factory_Microseconds(benchmark::State& state) {
41 while (state.KeepRunning()) {
46 BENCHMARK(BM_Duration_Factory_Microseconds);
48 void BM_Duration_Factory_Milliseconds(benchmark::State& state) {
50 while (state.KeepRunning()) {
55 BENCHMARK(BM_Duration_Factory_Milliseconds);
57 void BM_Duration_Factory_Seconds(benchmark::State& state) {
59 while (state.KeepRunning()) {
64 BENCHMARK(BM_Duration_Factory_Seconds);
66 void BM_Duration_Factory_Minutes(benchmark::State& state) {
68 while (state.KeepRunning()) {
73 BENCHMARK(BM_Duration_Factory_Minutes);
75 void BM_Duration_Factory_Hours(benchmark::State& state) {
77 while (state.KeepRunning()) {
82 BENCHMARK(BM_Duration_Factory_Hours);
84 void BM_Duration_Factory_DoubleNanoseconds(benchmark::State& state) {
86 while (state.KeepRunning()) {
88 d = d * 1.00000001 + 1;
91 BENCHMARK(BM_Duration_Factory_DoubleNanoseconds);
93 void BM_Duration_Factory_DoubleMicroseconds(benchmark::State& state) {
95 while (state.KeepRunning()) {
97 d = d * 1.00000001 + 1e-3;
100 BENCHMARK(BM_Duration_Factory_DoubleMicroseconds);
102 void BM_Duration_Factory_DoubleMilliseconds(benchmark::State& state) {
104 while (state.KeepRunning()) {
106 d = d * 1.00000001 + 1e-6;
109 BENCHMARK(BM_Duration_Factory_DoubleMilliseconds);
111 void BM_Duration_Factory_DoubleSeconds(benchmark::State& state) {
113 while (state.KeepRunning()) {
115 d = d * 1.00000001 + 1e-9;
118 BENCHMARK(BM_Duration_Factory_DoubleSeconds);
120 void BM_Duration_Factory_DoubleMinutes(benchmark::State& state) {
122 while (state.KeepRunning()) {
124 d = d * 1.00000001 + 1e-9;
127 BENCHMARK(BM_Duration_Factory_DoubleMinutes);
129 void BM_Duration_Factory_DoubleHours(benchmark::State& state) {
131 while (state.KeepRunning()) {
133 d = d * 1.00000001 + 1e-9;
136 BENCHMARK(BM_Duration_Factory_DoubleHours);
142 void BM_Duration_Addition(benchmark::State& state) {
145 while (state.KeepRunning()) {
146 benchmark::DoNotOptimize(d += step);
149 BENCHMARK(BM_Duration_Addition);
151 void BM_Duration_Subtraction(benchmark::State& state) {
154 while (state.KeepRunning()) {
155 benchmark::DoNotOptimize(d -= step);
158 BENCHMARK(BM_Duration_Subtraction);
160 void BM_Duration_Multiplication_Fixed(benchmark::State& state) {
164 while (state.KeepRunning()) {
165 benchmark::DoNotOptimize(s += d * (i + 1));
169 BENCHMARK(BM_Duration_Multiplication_Fixed);
171 void BM_Duration_Multiplication_Double(benchmark::State& state) {
175 while (state.KeepRunning()) {
176 benchmark::DoNotOptimize(s += d * (i + 1.0));
180 BENCHMARK(BM_Duration_Multiplication_Double);
182 void BM_Duration_Division_Fixed(benchmark::State& state) {
185 while (state.KeepRunning()) {
186 benchmark::DoNotOptimize(d /= i + 1);
190 BENCHMARK(BM_Duration_Division_Fixed);
192 void BM_Duration_Division_Double(benchmark::State& state) {
195 while (state.KeepRunning()) {
196 benchmark::DoNotOptimize(d /= i + 1.0);
200 BENCHMARK(BM_Duration_Division_Double);
202 void BM_Duration_FDivDuration_Nanoseconds(benchmark::State& state) {
205 while (state.KeepRunning()) {
206 benchmark::DoNotOptimize(
211 BENCHMARK(BM_Duration_FDivDuration_Nanoseconds);
213 void BM_Duration_IDivDuration_Nanoseconds(benchmark::State& state) {
217 while (state.KeepRunning()) {
218 benchmark::DoNotOptimize(a +=
224 BENCHMARK(BM_Duration_IDivDuration_Nanoseconds);
226 void BM_Duration_IDivDuration_Microseconds(benchmark::State& state) {
230 while (state.KeepRunning()) {
237 BENCHMARK(BM_Duration_IDivDuration_Microseconds);
239 void BM_Duration_IDivDuration_Milliseconds(benchmark::State& state) {
243 while (state.KeepRunning()) {
250 BENCHMARK(BM_Duration_IDivDuration_Milliseconds);
252 void BM_Duration_IDivDuration_Seconds(benchmark::State& state) {
256 while (state.KeepRunning()) {
257 benchmark::DoNotOptimize(
262 BENCHMARK(BM_Duration_IDivDuration_Seconds);
264 void BM_Duration_IDivDuration_Minutes(benchmark::State& state) {
268 while (state.KeepRunning()) {
269 benchmark::DoNotOptimize(
274 BENCHMARK(BM_Duration_IDivDuration_Minutes);
276 void BM_Duration_IDivDuration_Hours(benchmark::State& state) {
280 while (state.KeepRunning()) {
281 benchmark::DoNotOptimize(
286 BENCHMARK(BM_Duration_IDivDuration_Hours);
288 void BM_Duration_ToInt64Nanoseconds(benchmark::State& state) {
290 while (state.KeepRunning()) {
294 BENCHMARK(BM_Duration_ToInt64Nanoseconds);
296 void BM_Duration_ToInt64Microseconds(benchmark::State& state) {
298 while (state.KeepRunning()) {
302 BENCHMARK(BM_Duration_ToInt64Microseconds);
304 void BM_Duration_ToInt64Milliseconds(benchmark::State& state) {
306 while (state.KeepRunning()) {
310 BENCHMARK(BM_Duration_ToInt64Milliseconds);
312 void BM_Duration_ToInt64Seconds(benchmark::State& state) {
314 while (state.KeepRunning()) {
318 BENCHMARK(BM_Duration_ToInt64Seconds);
320 void BM_Duration_ToInt64Minutes(benchmark::State& state) {
322 while (state.KeepRunning()) {
326 BENCHMARK(BM_Duration_ToInt64Minutes);
328 void BM_Duration_ToInt64Hours(benchmark::State& state) {
330 while (state.KeepRunning()) {
334 BENCHMARK(BM_Duration_ToInt64Hours);
340 void BM_Duration_ToTimespec_AbslTime(benchmark::State& state) {
342 while (state.KeepRunning()) {
346 BENCHMARK(BM_Duration_ToTimespec_AbslTime);
351 ts.tv_nsec = (seconds - ts.tv_sec) * (1000 * 1000 * 1000);
355 void BM_Duration_ToTimespec_Double(benchmark::State& state) {
356 while (state.KeepRunning()) {
357 benchmark::DoNotOptimize(DoubleToTimespec(1.0));
360 BENCHMARK(BM_Duration_ToTimespec_Double);
362 void BM_Duration_FromTimespec_AbslTime(benchmark::State& state) {
366 while (state.KeepRunning()) {
367 if (++ts.tv_nsec == 1000 * 1000 * 1000) {
374 BENCHMARK(BM_Duration_FromTimespec_AbslTime);
377 return ts.tv_sec + (ts.tv_nsec / (1000 * 1000 * 1000));
380 void BM_Duration_FromTimespec_Double(benchmark::State& state) {
384 while (state.KeepRunning()) {
385 if (++ts.tv_nsec == 1000 * 1000 * 1000) {
389 benchmark::DoNotOptimize(TimespecToDouble(ts));
392 BENCHMARK(BM_Duration_FromTimespec_Double);
398 const char*
const kDurations[] = {
403 "2562047788015215h30m7.99999999975s",
405 const int kNumDurations =
sizeof(kDurations) /
sizeof(kDurations[0]);
407 void BM_Duration_FormatDuration(benchmark::State& state) {
408 const std::string s = kDurations[state.range(0)];
412 while (state.KeepRunning()) {
416 BENCHMARK(BM_Duration_FormatDuration)->DenseRange(0, kNumDurations - 1);
418 void BM_Duration_ParseDuration(benchmark::State& state) {
419 const std::string s = kDurations[state.range(0)];
422 while (state.KeepRunning()) {
426 BENCHMARK(BM_Duration_ParseDuration)->DenseRange(0, kNumDurations - 1);
timespec ToTimespec(Duration d)
int64_t ToInt64Minutes(Duration d)
constexpr Duration Hours(int64_t n)
Duration DurationFromTimespec(timespec ts)
int64_t ToInt64Microseconds(Duration d)
CONSTEXPR_F fields step(second_tag, fields f, diff_t n) noexcept
constexpr Duration Microseconds(int64_t n)
std::chrono::duration< std::int_fast64_t > seconds
constexpr Duration Milliseconds(int64_t n)
int64_t ToInt64Nanoseconds(Duration d)
int64_t ToInt64Seconds(Duration d)
int64_t ToInt64Hours(Duration d)
std::string FormatDuration(Duration d)
int64_t IDivDuration(Duration num, Duration den, Duration *rem)
constexpr Duration Minutes(int64_t n)
#define ABSL_ATTRIBUTE_NOINLINE
int64_t ToInt64Milliseconds(Duration d)
double FDivDuration(Duration num, Duration den)
constexpr Duration Seconds(int64_t n)
constexpr Duration Nanoseconds(int64_t n)
bool ParseDuration(const std::string &dur_string, Duration *d)