Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Timer.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
22#ifndef __MACH__
23 #define USE_PLF_TIMER
24#endif
25
26#if defined(USE_PLF_TIMER)
27 #include <plf_nanotimer.h>
28#else
29 #include <chrono>
30#endif
31
32namespace shambase {
36 class Timer {
37 public:
38#if defined(USE_PLF_TIMER)
39 plf::nanotimer timer;
40#else
41 std::chrono::steady_clock::time_point t_start;
42#endif
44
46 Timer() : nanosec(0.0) {}
47
51 inline void start() {
52#if defined(USE_PLF_TIMER)
53 timer.start();
54#else
55 t_start = std::chrono::steady_clock::now();
56#endif
57 }
58
65 inline void stop() {
66#if defined(USE_PLF_TIMER)
67 nanosec = f64(timer.get_elapsed_ns());
68#else
69 auto t_end = std::chrono::steady_clock::now();
70 nanosec = f64(
71 std::chrono::duration_cast<std::chrono::nanoseconds>(t_end - t_start).count());
72#endif
73 }
74
79 inline std::string get_time_str() const {
80 auto res = sham::to_human_readable(nanosec * 1e-9);
81 return sham::format("{:.2f} {}s", res.value, res.prefix);
82 }
83
88 [[nodiscard]] inline f64 elapsed_sec() const { return nanosec * 1e-9; }
89 };
90} // namespace shambase
double f64
Alias for double.
plf::nanotimer timer
Internal timer.
Definition Timer.hpp:39
std::string get_time_str() const
Converts the stored nanosecond time to a string representation.
Definition Timer.hpp:79
Timer()
Constructor, init nanosec to 0.
Definition Timer.hpp:46
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
f64 nanosec
Time in nanoseconds.
Definition Timer.hpp:43
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