-
Notifications
You must be signed in to change notification settings - Fork 25
/
raiitimer.h
67 lines (52 loc) · 1.91 KB
/
raiitimer.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
/*
* Copyright (C) 2016 - 2024 Judd Niemann - All Rights Reserved.
* You may use, distribute and modify this code under the
* terms of the GNU Lesser General Public License, version 2.1
*
* You should have received a copy of GNU Lesser General Public License v2.1
* with this file. If not, please refer to: https://github.com/jniemann66/ReSampler
*/
#ifndef _RAIITIMER_H
#define _RAIITIMER_H 1
#include <iostream>
#include <iomanip>
#include <chrono>
// class RaiiTimer : starts a high-resolution timer upon construction and prints elapsed time to stdout upon destruction
// For convenience, a reference time value (in ms) for comparison may be provided using the parameter msComparison.
namespace ReSampler {
class RaiiTimer
{
public:
explicit RaiiTimer(double msComparison = 0.0) : msComparison(msComparison) {
beginTimer = std::chrono::high_resolution_clock::now();
}
~RaiiTimer() {
endTimer = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTimer - beginTimer).count();
std::cout << "Time=" << duration << " ms";
if(msComparison != 0.0) {
double relativeSpeed = msComparison / duration;
auto ss = std::cout.precision();
std::cout << " [" << std::setprecision(1) << relativeSpeed << "x]" << std::setprecision(
static_cast<int>(ss));
}
std::cout << "\n" << std::endl;
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> beginTimer;
std::chrono::time_point<std::chrono::high_resolution_clock> endTimer;
double msComparison;
};
// utility
class TS
{
public:
static void elapsedSince(int64_t t) {
std::cout << microsecondsSinceEpoch() - t << "μS" << std::endl;
}
static int64_t microsecondsSinceEpoch() {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
};
} // namespace ReSampler
#endif // _RAIITIMER_H