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
19#include "shambase/Timer.hpp"
21#include <functional>
22
23namespace shambase {
24
36 f64 acc = 0;
38 u32 run_count = 0;
39
40 public:
46 template<class Func>
47 void time_func(Func &&f) {
48 Timer t;
49 t.start();
50 f();
51 t.stop();
52 acc += t.elapsed_sec();
53 run_count += 1;
54 }
55
61 inline f64 func_time_sec() { return acc / run_count; }
62 };
63
76 template<class Func>
77 inline f64 timeit(Func &&f, u32 relaunch = 1) {
78
80
81 for (u32 i = 0; i < relaunch; i++) {
82 t.time_func([&]() {
83 f();
84 });
85 }
86
87 return t.func_time_sec();
88 }
89
104 template<class Func>
105 inline f64 timeitfor(Func &&f, f64 max_duration = 1) {
106
108 Timer tdur;
109 tdur.start();
110 do {
111 t.time_func([&]() {
112 f();
113 });
114 tdur.stop();
115 } while (tdur.elapsed_sec() < max_duration);
116
117 return t.func_time_sec();
118 }
119
131 std::vector<f64> counts;
132
136 std::vector<f64> times;
137 };
138
153 std::function<f64(u32)> func, u32 start, u32 end, f64 pow_exp) {
154 BenchmarkResult res;
155 for (f64 i = start; i < end; i *= pow_exp) {
156 res.counts.push_back(i);
157 res.times.push_back(func(u32(i)));
158 }
159
160 return res;
161 }
162
163} // namespace shambase
High-resolution timer with plf_nanotimer (non-macOS) or std::chrono (macOS) backend.
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:34
f64 func_time_sec()
Returns the average time it takes to execute the function.
Definition time.hpp:61
void time_func(Func &&f)
Measures the time it takes to execute a function.
Definition time.hpp:47
Class Timer measures the time elapsed since the timer was started.
Definition Timer.hpp:36
f64 elapsed_sec() const
Converts the stored nanosecond time to a floating point representation in seconds.
Definition Timer.hpp:88
void start()
Starts the timer.
Definition Timer.hpp:51
void stop()
Stops the timer and stores the elapsed time in nanoseconds.
Definition Timer.hpp:65
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:105
f64 timeit(Func &&f, u32 relaunch=1)
Measures the average time it takes to execute a function.
Definition time.hpp:77
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:152
Structure to store the results of a benchmark.
Definition time.hpp:127
std::vector< f64 > counts
The counts of the benchmark.
Definition time.hpp:131
std::vector< f64 > times
The times of the benchmark.
Definition time.hpp:136