-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf-timer.h
94 lines (69 loc) · 3.53 KB
/
perf-timer.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// perf-timer.h
//
#pragma once
//
//#include <iostream>
#include <chrono>
#include <algorithm>
//
////////////////////////////////////////////////////////////////////////////////
using namespace std::chrono_literals;
template <typename Time = std::chrono::nanoseconds,
typename Clock = std::chrono::steady_clock> //std::chrono::high_resolution_clock>
struct perftimer_t {
// returns time measured in Time, defaulting to nanoseconds
template <typename F, typename... Args>
static inline auto duration(F&& f, Args&&... args) -> Time {
const typename Clock::time_point start { Clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const typename Clock::time_point end { Clock::now() };
return std::chrono::duration_cast<Time>(end - start);
}
// returns time measured in seconds
template <typename F, typename... Args>
static inline auto duration_secs(F&& f, Args&&... args) -> std::chrono::duration<double> {
const typename Clock::time_point start { Clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const typename Clock::time_point end { Clock::now() };
return std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
}
}; // struct perftimer_t
// returns time measured in seconds
template <typename F, typename... Args>
static inline auto duration_secs(F&& f, Args&&... args) -> double {
const std::chrono::steady_clock::time_point start { std::chrono::steady_clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const std::chrono::steady_clock::time_point end { std::chrono::steady_clock::now() };
return std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count();
}
// returns time measured in nanoseconds
template <typename F, typename... Args>
static inline auto duration_nsec(F&& f, Args&&... args) -> long {
const std::chrono::steady_clock::time_point start { std::chrono::steady_clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const std::chrono::steady_clock::time_point end { std::chrono::steady_clock::now() };
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
// returns time measured in microseconds
template <typename F, typename... Args>
static inline auto duration_microsec(F&& f, Args&&... args) -> long {
const std::chrono::steady_clock::time_point start { std::chrono::steady_clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const std::chrono::steady_clock::time_point end { std::chrono::steady_clock::now() };
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
}
// returns time measured in milliseconds
template <typename F, typename... Args>
static inline auto duration_msec(F&& f, Args&&... args) -> long {
const std::chrono::steady_clock::time_point start { std::chrono::steady_clock::now() };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...); // from C++17
//f(std::forward<Args>(args)...); // until C++17
const std::chrono::steady_clock::time_point end { std::chrono::steady_clock::now() };
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}