-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifetimeClock.h
More file actions
89 lines (65 loc) · 3.22 KB
/
LifetimeClock.h
File metadata and controls
89 lines (65 loc) · 3.22 KB
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
#pragma once
#include <iostream>
#include <chrono>
template<class T = std::chrono::seconds>
class LifetimeClock {
public:
LifetimeClock() = default;
explicit LifetimeClock(bool doNotPrint) : DoNotPrint(doNotPrint) {}
explicit LifetimeClock(std::string prefix, std::string suffix, bool doNotPrint = false) : Prefix(std::move(prefix)),
Suffix(std::move(suffix)),
DoNotPrint(doNotPrint) {}
~LifetimeClock() {
if (!DoNotPrint) {
PrintTimeRunning();
}
}
void PrintTimeRunning() const {
const auto end = std::chrono::system_clock::now();
const auto elapsedTime = std::chrono::duration_cast<T>
(end - start).count();
std::cout << Prefix << elapsedTime << Suffix;
}
auto GetElapsedTime() const {
const auto end = std::chrono::system_clock::now();
return std::chrono::duration_cast<T>
(end - start).count();
}
std::string Prefix = "Elapsed time: ";
std::string Suffix = "\n";
bool DoNotPrint;
private:
std::chrono::time_point <std::chrono::system_clock> start = std::chrono::system_clock::now();
};
class LifetimeClock_Nanoseconds : public LifetimeClock<std::chrono::nanoseconds> {
public:
explicit LifetimeClock_Nanoseconds() : LifetimeClock("Elapsed time: ", "ns\n") {}
explicit LifetimeClock_Nanoseconds(bool doNotPrint) :
LifetimeClock("Elapsed time: ", "ns\n", doNotPrint) {}
explicit LifetimeClock_Nanoseconds(std::string prefix, std::string suffix, bool doNotPrint = false) :
LifetimeClock(std::move(prefix), std::move(suffix), doNotPrint) {}
};
class LifetimeClock_Seconds : public LifetimeClock<std::chrono::seconds> {
public:
explicit LifetimeClock_Seconds() : LifetimeClock("Elapsed time: ", "s\n") {}
explicit LifetimeClock_Seconds(bool doNotPrint) :
LifetimeClock("Elapsed time: ", "s\n", doNotPrint) {}
explicit LifetimeClock_Seconds(std::string prefix, std::string suffix, bool doNotPrint = false) :
LifetimeClock(std::move(prefix), std::move(suffix), doNotPrint) {}
};
class LifetimeClock_Micoseconds : public LifetimeClock<std::chrono::microseconds> {
public:
explicit LifetimeClock_Micoseconds() : LifetimeClock("Elapsed time: ", "µs\n") {}
explicit LifetimeClock_Micoseconds(bool doNotPrint) :
LifetimeClock("Elapsed time: ", "s\n", doNotPrint) {}
explicit LifetimeClock_Micoseconds(std::string prefix, std::string suffix, bool doNotPrint = false) :
LifetimeClock(std::move(prefix), std::move(suffix), doNotPrint) {}
};
class LifetimeClock_Milliseconds : public LifetimeClock<std::chrono::milliseconds> {
public:
explicit LifetimeClock_Milliseconds() : LifetimeClock("Elapsed time: ", "ms\n") {}
explicit LifetimeClock_Milliseconds(bool doNotPrint) :
LifetimeClock("Elapsed time: ", "s\n", doNotPrint) {}
explicit LifetimeClock_Milliseconds(std::string prefix, std::string suffix, bool doNotPrint = false) :
LifetimeClock(std::move(prefix), std::move(suffix), doNotPrint) {}
};