Tatooine
chrono.h
Go to the documentation of this file.
1#ifndef TATOOINE_CHRONO_H
2#define TATOOINE_CHRONO_H
3//==============================================================================
4#include <chrono>
5#include <utility>
6#include <tuple>
7//==============================================================================
8namespace tatooine {
9//==============================================================================
10template <typename F, typename... Param>
11auto measure(F&& f, Param&&... param) {
12 using time = std::chrono::high_resolution_clock;
13 using f_return_type = decltype(f(std::forward<Param>(param)...));
14
15 auto before = time::now();
16
17 if constexpr (std::is_same_v<f_return_type, void>) {
18 f(std::forward<Param>(param)...);
19 return time::now() - before;
20
21 } else {
22 decltype(auto) ret = f(std::forward<Param>(param)...);
23 auto duration = time::now() - before;
24 return std::pair<decltype(duration), decltype(ret)>{duration, ret};
25 }
26}
27//------------------------------------------------------------------------------
28template <typename... Durations, typename DurationIn>
29auto break_down_durations(DurationIn d) {
30 using namespace std::chrono;
31 std::tuple<Durations...> retval;
32 (((std::get<Durations>(retval) = duration_cast<Durations>(d)),
33 (d -= duration_cast<DurationIn>(std::get<Durations>(retval)))),
34 ...);
35 return retval;
36}
37//==============================================================================
38} // namespace tatooine
39//==============================================================================
40#endif
Definition: algorithm.h:6
auto break_down_durations(DurationIn d)
Definition: chrono.h:29
auto measure(F &&f, Param &&... param)
Definition: chrono.h:11