-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime_lapse.h
61 lines (49 loc) · 1.44 KB
/
time_lapse.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
57
58
59
60
61
//
// Created by jeremie on 15/10/18.
//
#ifndef CDATAHUSTLE_TIME_LAPSE_H
#define CDATAHUSTLE_TIME_LAPSE_H
#include <chrono>
#define fw(what) std::forward<decltype(what)>(what)
#define NB_ITERATION 5
/**
* @ class measure
* @ brief Class to measure the execution time of a callable
*/
template <
typename TimeT = std::chrono::milliseconds, class ClockT = std::chrono::system_clock
>
struct measure
{
/**
* @ fn execution
* @ brief Returns the quantity (count) of the elapsed time as TimeT units
*/
template<typename F, typename ...Args>
static typename TimeT::rep execution(F&& func, Args&&... args)
{
auto min = INT64_MAX;
for (auto i = 0; i < NB_ITERATION; i++){
auto start = ClockT::now();
fw(func)(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast<TimeT>(ClockT::now() - start).count();
if(duration < min){
min = duration;
}
}
return min;
}
/**
* @ fn duration
* @ brief Returns the duration (in chrono's type system) of the elapsed time
*/
template<typename F, typename... Args>
static TimeT duration(F&& func, Args&&... args)
{
auto start = ClockT::now();
fw(func)(std::forward<Args>(args)...);
return std::chrono::duration_cast<TimeT>(ClockT::now() - start);
}
};
#undef NB_ITERATION
#endif //CDATAHUSTLE_TIME_LAPSE_H