cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
monotone_time_point.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <chrono>
23
25
26#if defined(STAT_BENCH_HAS_WIN_MONOTONE_CLOCK)
27
28#include <cstdint>
29
30// clang-format off
31#include <windows.h>
32#include <profileapi.h>
33// clang-format on
34
35namespace stat_bench {
36namespace clock {
37
43auto win_monotone_clock_frequency() noexcept -> std::int64_t {
44 LARGE_INTEGER freq;
45 // This never fails
46 // (https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency).
47 QueryPerformanceFrequency(&freq);
48 return freq.QuadPart;
49}
50
52 const WinMonotoneTimePoint& right) const noexcept -> Duration {
53 static const double resolution =
54 1.0 / static_cast<double>(win_monotone_clock_frequency());
55 return Duration(resolution * static_cast<double>(data_ - right.data_));
56}
57
59 LARGE_INTEGER ticks;
60 // This never fails
61 // (https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter?redirectedfrom=MSDN).
62 QueryPerformanceCounter(&ticks);
63 return WinMonotoneTimePoint(ticks.QuadPart);
64}
65
67 static const double resolution =
68 1.0 / static_cast<double>(win_monotone_clock_frequency());
69 return Duration(resolution);
70}
71
72WinMonotoneTimePoint::WinMonotoneTimePoint(DataType data) noexcept
73 : data_(data) {}
74
75} // namespace clock
76} // namespace stat_bench
77
78#endif
79
80#ifdef STAT_BENCH_HAS_UNIX_MONOTONE_CLOCK
81
82#include <cstdio>
83#include <cstdlib>
84
85#include <time.h> // NOLINT: For clock_gettime and cloock_getres.
86
87#define STAT_BENCH_CLOCK_ID CLOCK_MONOTONIC
88
89namespace stat_bench {
90namespace clock {
91
93 const UnixMonotoneTimePoint& right) const noexcept -> Duration {
94 constexpr double nsec_to_sec = 1e-9;
95 return Duration(static_cast<double>(data_.tv_sec - right.data_.tv_sec) +
96 nsec_to_sec * static_cast<double>(data_.tv_nsec - right.data_.tv_nsec));
97}
98
100 timespec ts{};
101 if (clock_gettime(STAT_BENCH_CLOCK_ID, &ts) != 0) {
102 std::perror("Failed to get the current tick.");
103 std::abort();
104 }
105 return UnixMonotoneTimePoint(ts);
106}
107
109 timespec ts{};
110 if (clock_getres(STAT_BENCH_CLOCK_ID, &ts) != 0) {
111 std::perror("Failed to get the resolution of the clock.");
112 std::abort();
113 }
114 constexpr double nsec_to_sec = 1e-9;
115 return Duration(static_cast<double>(ts.tv_sec) +
116 nsec_to_sec * static_cast<double>(ts.tv_nsec));
117}
118
119UnixMonotoneTimePoint::UnixMonotoneTimePoint(DataType data) noexcept
120 : data_(data) {}
121
122} // namespace clock
123} // namespace stat_bench
124
125#endif
126
127namespace stat_bench {
128namespace clock {
129
131 const StdMonotoneTimePoint& right) const noexcept -> Duration {
132 return Duration(std::chrono::duration_cast<std::chrono::duration<double>>(
133 data_ - right.data_)
134 .count());
135}
136
137auto StdMonotoneTimePoint::now() noexcept -> StdMonotoneTimePoint {
138 return StdMonotoneTimePoint(std::chrono::steady_clock::now());
139}
140
142 // This may not be true.
143 return Duration(
144 static_cast<double>(std::chrono::steady_clock::duration::period::num) /
145 static_cast<double>(std::chrono::steady_clock::duration::period::den));
146}
147
148StdMonotoneTimePoint::StdMonotoneTimePoint(DataType data) noexcept
149 : data_(data) {}
150
151} // namespace clock
152} // namespace stat_bench
Class of durations.
Definition duration.h:28
auto operator-(const StdMonotoneTimePoint &right) const noexcept -> Duration
Calculate a duration between two time points.
static auto now() noexcept -> StdMonotoneTimePoint
Get the current time from the monotone clock.
static auto resolution() noexcept -> Duration
Get the resolution of the monotone clock.
Class of time points of the monotone clock in Unix.
static auto resolution() noexcept -> Duration
Get the resolution of the monotone clock.
static auto now() noexcept -> UnixMonotoneTimePoint
Get the current time from the monotone clock.
auto operator-(const UnixMonotoneTimePoint &right) const noexcept -> Duration
Calculate a duration between two time points.
Class of time points of the monotone clock in Windows.
static auto resolution() noexcept -> Duration
Get the resolution of the monotone clock.
static auto now() noexcept -> WinMonotoneTimePoint
Get the current time from the monotone clock.
auto operator-(const WinMonotoneTimePoint &right) const noexcept -> Duration
Calculate a duration between two time points.
Definition of Duration class.
Definition of classes of monotone clocks.
Namespace of clocks for benchmarks.
Definition duration.h:23
Namespace of stat_bench source codes.