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
20#include "shambase/string.hpp"
21#include <plf_nanotimer.h>
22#include <functional>
23#include <iostream>
24
25namespace shambase {
26
33 inline std::string nanosec_to_time_str(double nanosec) {
34 double sec_int = nanosec;
35
36 std::string unit = "ns";
37
38 if (sec_int > 2000) {
39 unit = "us";
40 sec_int /= 1000;
41 }
42
43 if (sec_int > 2000) {
44 unit = "ms";
45 sec_int /= 1000;
46 }
47
48 if (sec_int > 2000) {
49 unit = "s";
50 sec_int /= 1000;
51 }
52
53 if (sec_int > 2000) {
54 unit = "ks";
55 sec_int /= 1000;
56 }
57
58 if (sec_int > 2000) {
59 unit = "Ms";
60 sec_int /= 1000;
61 }
62
63 if (sec_int > 2000) {
64 unit = "Gs";
65 sec_int /= 1000;
66 }
67
68 return shambase::format_printf("%4.2f", sec_int) + " " + unit;
69 }
70
71#ifdef __MACH__
72 class Timer {
73 public:
74 std::chrono::steady_clock::time_point t_start, t_end;
76
77 Timer() {};
78
79 inline void start() { t_start = std::chrono::steady_clock::now(); }
80
81 inline void end() {
82 t_end = std::chrono::steady_clock::now();
83 nanosec = f64(
84 std::chrono::duration_cast<std::chrono::nanoseconds>(t_end - t_start).count());
85 }
86
87 inline std::string get_time_str() { return nanosec_to_time_str(nanosec); }
88
89 inline f64 elasped_sec() { return f64(nanosec) * 1e-9; }
90 };
91#else
92
96 class Timer {
97 public:
98 plf::nanotimer timer;
99
101
102 Timer() {};
106 inline void start() { timer.start(); }
107
111 inline void end() { nanosec = timer.get_elapsed_ns(); }
112
117 inline std::string get_time_str() const { return nanosec_to_time_str(nanosec); }
118
123 [[nodiscard]] inline f64 elasped_sec() const { return f64(nanosec) * 1e-9; }
124 };
125#endif
126
138 f64 acc = 0;
140 u32 run_count = 0;
141
142 public:
148 template<class Func>
149 void time_func(Func &&f) {
150 Timer t;
151 t.start();
152 f();
153 t.end();
154 acc += t.elasped_sec();
155 run_count += 1;
156 }
157
163 inline f64 func_time_sec() { return acc / run_count; }
164 };
165
178 template<class Func>
179 inline f64 timeit(Func &&f, u32 relaunch = 1) {
180
182
183 for (u32 i = 0; i < relaunch; i++) {
184 t.time_func([&]() {
185 f();
186 });
187 }
188
189 return t.func_time_sec();
190 }
191
206 template<class Func>
207 inline f64 timeitfor(Func &&f, f64 max_duration = 1) {
208
210 Timer tdur;
211 tdur.start();
212 do {
213 t.time_func([&]() {
214 f();
215 });
216 tdur.end();
217 } while (tdur.elasped_sec() < max_duration);
218
219 return t.func_time_sec();
220 }
221
233 std::vector<f64> counts;
234
238 std::vector<f64> times;
239 };
240
255 std::function<f64(u32)> func, u32 start, u32 end, f64 pow_exp) {
257 for (f64 i = start; i < end; i *= pow_exp) {
258 res.counts.push_back(i);
259 res.times.push_back(func(u32(i)));
260 }
261
262 return res;
263 }
264
265} // 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:136
f64 func_time_sec()
Returns the average time it takes to execute the function.
Definition time.hpp:163
void time_func(Func &&f)
Measures the time it takes to execute a function.
Definition time.hpp:149
Class Timer measures the time elapsed since the timer was started.
Definition time.hpp:96
plf::nanotimer timer
Internal timer.
Definition time.hpp:98
std::string get_time_str() const
Converts the stored nanosecond time to a string representation.
Definition time.hpp:117
void end()
Stops the timer and stores the elapsed time in nanoseconds.
Definition time.hpp:111
f64 elasped_sec() const
Converts the stored nanosecond time to a floating point representation in seconds.
Definition time.hpp:123
void start()
Starts the timer.
Definition time.hpp:106
f64 nanosec
Time in nanosecond.
Definition time.hpp:100
namespace for basic c++ utilities
std::string nanosec_to_time_str(double nanosec)
Convert nanoseconds to a human-readable string representation.
Definition time.hpp:33
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:207
f64 timeit(Func &&f, u32 relaunch=1)
Measures the average time it takes to execute a function.
Definition time.hpp:179
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:254
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
Structure to store the results of a benchmark.
Definition time.hpp:229
std::vector< f64 > counts
The counts of the benchmark.
Definition time.hpp:233
std::vector< f64 > times
The times of the benchmark.
Definition time.hpp:238