-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctimeit.h
56 lines (45 loc) · 1.6 KB
/
ctimeit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef CTIMEIT_H
#define CTIMEIT_H
#include <chrono>
#include <cmath>
#include <iostream>
#include <limits>
namespace ctimeit {
using GetTime = std::chrono::steady_clock;
using std::chrono::duration_cast;
std::string format_time(int64_t);
template <size_t N = 1000, typename Callable, typename... Args>
void timeit(Callable func, Args&&... Funcargs) {
/*
* Measure the average execution time of `func` which takes `Funcargs`
* after `N` executions.
*/
double total_time{0};
int64_t min_exec_time{std::numeric_limits<int64_t>::max()}, max_exec_time{0};
for (size_t i = 0; i < N; ++i) {
auto start = GetTime::now();
func(std::forward<Args>(Funcargs)...);
auto end = GetTime::now();
auto run_time =
duration_cast<std::chrono::nanoseconds>(end - start).count();
min_exec_time = std::min(min_exec_time, run_time);
max_exec_time = std::max(max_exec_time, run_time);
total_time += run_time;
}
std::cout << "Average time taken : " << format_time(total_time / N) << " ("
<< N << " runs)\n"
<< "Max time taken : " << format_time(max_exec_time) << "\n"
<< "Min time taken : " << format_time(min_exec_time) << "\n";
}
std::string format_time(double run_time) {
/*
* For setting the scale of execution time.
*/
double scaling[]{1, 1e3, 1e6, 1e9};
int pow = std::floor(std::log10(run_time));
std::array formats = {"ns", "µs", "ms", "s"};
auto idx = std::clamp(pow / 3, 0, static_cast<int>(formats.size() - 1));
return std::to_string(run_time / scaling[idx]) + formats[idx];
}
} // namespace ctimeit
#endif // CTIMEIT_H