Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
20#include "shambase/string.hpp"
21#include <functional>
22#include <iostream>
23
24#ifndef __MACH__
25 #include <plf_nanotimer.h>
26#else
27 #include <chrono>
28#endif
29
30namespace shambase {
31
35 class Timer {
36 public:
37#if defined(__MACH__)
38 std::chrono::steady_clock::time_point t_start;
39#else
40 plf::nanotimer timer;
41#endif
43
45 Timer() : nanosec(0.0) {}
46
50 inline void start() {
51#if defined(__MACH__)
52 t_start = std::chrono::steady_clock::now();
53#else
54 timer.start();
55#endif
56 }
57
64 inline void stop() {
65#if defined(__MACH__)
66 auto t_end = std::chrono::steady_clock::now();
67 nanosec = f64(
68 std::chrono::duration_cast<std::chrono::nanoseconds>(t_end - t_start).count());
69#else
70 nanosec = f64(timer.get_elapsed_ns());
71#endif
72 }
73
78 inline std::string get_time_str() const {
79 auto res = sham::to_human_readable(nanosec * 1e-9);
80 return sham::format("{:.2f} {}s", res.value, res.prefix);
81 }
82
87 [[nodiscard]] inline f64 elapsed_sec() const { return nanosec * 1e-9; }
88 };
89
101 f64 acc = 0;
103 u32 run_count = 0;
104
105 public:
111 template<class Func>
112 void time_func(Func &&f) {
113 Timer t;
114 t.start();
115 f();
116 t.stop();
117 acc += t.elapsed_sec();
118 run_count += 1;
119 }
120
126 inline f64 func_time_sec() { return acc / run_count; }
127 };
128
141 template<class Func>
142 inline f64 timeit(Func &&f, u32 relaunch = 1) {
143
145
146 for (u32 i = 0; i < relaunch; i++) {
147 t.time_func([&]() {
148 f();
149 });
150 }
151
152 return t.func_time_sec();
153 }
154
169 template<class Func>
170 inline f64 timeitfor(Func &&f, f64 max_duration = 1) {
171
173 Timer tdur;
174 tdur.start();
175 do {
176 t.time_func([&]() {
177 f();
178 });
179 tdur.stop();
180 } while (tdur.elapsed_sec() < max_duration);
181
182 return t.func_time_sec();
183 }
184
196 std::vector<f64> counts;
197
201 std::vector<f64> times;
202 };
203
218 std::function<f64(u32)> func, u32 start, u32 end, f64 pow_exp) {
219 BenchmarkResult res;
220 for (f64 i = start; i < end; i *= pow_exp) {
221 res.counts.push_back(i);
222 res.times.push_back(func(u32(i)));
223 }
224
225 return res;
226 }
227
228} // namespace shambase
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
Class FunctionTimer measures the time it takes to execute a function.
Definition time.hpp:99
f64 func_time_sec()
Returns the average time it takes to execute the function.
Definition time.hpp:126
void time_func(Func &&f)
Measures the time it takes to execute a function.
Definition time.hpp:112
Class Timer measures the time elapsed since the timer was started.
Definition time.hpp:35
plf::nanotimer timer
Internal timer.
Definition time.hpp:40
std::string get_time_str() const
Converts the stored nanosecond time to a string representation.
Definition time.hpp:78
Timer()
Constructor, init nanosec to 0.
Definition time.hpp:45
f64 elapsed_sec() const
Converts the stored nanosecond time to a floating point representation in seconds.
Definition time.hpp:87
void start()
Starts the timer.
Definition time.hpp:50
void stop()
Stops the timer and stores the elapsed time in nanoseconds.
Definition time.hpp:64
f64 nanosec
Time in nanoseconds.
Definition time.hpp:42
human_readable_t to_human_readable(double value)
Convert a raw value to a human-readable scaled form with an SI prefix.
namespace for basic c++ utilities
f64 timeitfor(Func &&f, f64 max_duration=1)
Measures the average time it takes to execute a function until a maximum duration is reached.
Definition time.hpp:170
f64 timeit(Func &&f, u32 relaunch=1)
Measures the average time it takes to execute a function.
Definition time.hpp:142
BenchmarkResult benchmark_pow_len(std::function< f64(u32)> func, u32 start, u32 end, f64 pow_exp)
Benchmark a function with input values following a power law.
Definition time.hpp:217
Structure to store the results of a benchmark.
Definition time.hpp:192
std::vector< f64 > counts
The counts of the benchmark.
Definition time.hpp:196
std::vector< f64 > times
The times of the benchmark.
Definition time.hpp:201