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
95
96
97
98
99
| #include <chrono>
#include <iostream>
#include <iomanip>
#include <thread>
#include <ctime>
#include <string>
#include <vector>
#include <numeric>
template <typename T>
void printClockData()
{
using PeriodType = typename T::period;
std::cout << " - precision: ";
std::cout << static_cast<double>(PeriodType::num) / PeriodType::den << " seconds" << std::endl;
std::cout << " - is_steady: " << std::boolalpha << T::is_steady << std::endl;
}
std::string asString(const std::chrono::system_clock::time_point& tp)
{
constexpr bool blUseLocalTime = false;
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::string ts = blUseLocalTime ? std::ctime(&t) : std::asctime(gmtime(&t));
ts.resize(ts.size() - 1);
return ts;
}
volatile int sink;
int main()
{
/* Clock */
std::cout << "system_clock: " << std::endl;
printClockData<std::chrono::system_clock>();
std::cout << "\nsteady_clock: " << std::endl;
printClockData<std::chrono::steady_clock>();
std::cout << "\nhigh_resolution_clock: " << std::endl;
printClockData<std::chrono::high_resolution_clock>();
/* system_clock */
auto system_start = std::chrono::system_clock::now();
// do some heavy work here, more than five seconds
using namespace std::chrono_literals;
// std::this_thread::sleep_for(6s);
if (std::chrono::system_clock::now() > system_start + std::chrono::seconds(5)) {
// If someone adjust the clock, condition may produce "false"
std::cout << "Checked!" << std::endl;
}
auto diffSec = std::chrono::system_clock::now() - system_start;
auto sec = std::chrono::duration_cast<std::chrono::milliseconds>(diffSec);
std::cout << "This program runs: " << sec.count() << " milliseconds" << std::endl;
/* steady_clock */
for (auto size = 1ull; size < 1000000000ull; size *= 100) {
auto start = std::chrono::steady_clock::now();
std::vector<int> Vec(size, 123);
sink = std::accumulate(Vec.begin(), Vec.end(), 0u); // make sure it's a side effect
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diffSec = end - start;
std::cout << "Time to fill and iterate a vector of " << size << " ints : " << diffSec.count() << " s\n";
}
/* Time Point */
std::chrono::system_clock::time_point tp;
auto sysClockNow = std::chrono::system_clock::now();
auto sysClockMin = std::chrono::system_clock::time_point::min();
auto sysClockMax = std::chrono::system_clock::time_point::max();
std::cout << "epoch: " << asString(tp) << std::endl;
std::cout << "current time: " << asString(sysClockNow) << std::endl;
std::cout << "minimum time: " << asString(sysClockMin) << std::endl;
std::cout << "maximum time: " << asString(sysClockMax) << std::endl;
/* Timepoint and Duration Arithmetic */
using TypeDays = std::chrono::duration<int, std::ratio<3600 * 24>>;
std::chrono::time_point<std::chrono::system_clock> timePoint;
std::cout << "epoch: " << asString(tp) << std::endl;
tp += TypeDays(1) + std::chrono::hours(23) + std::chrono::minutes(55);
auto diff = tp - std::chrono::system_clock::time_point();
auto minutesDiff = std::chrono::duration_cast<std::chrono::minutes>(diff);
TypeDays daysDiff = std::chrono::duration_cast<TypeDays>(diff);
std::cout << "later: " << asString(tp) << std::endl;
std::cout << "diff: " << minutesDiff.count() << " minute(s)" << std::endl;
std::cout << "diff: " << daysDiff.count() << " day(s)" << std::endl;
tp -= std::chrono::hours(24 * 365);
std::cout << "-1 year: " << asString(tp) << std::endl;
tp -= std::chrono::duration<int, std::ratio<3600 * 24 * 365>>(50);
std::cout << "-50 years: " << asString(tp) << std::endl;
tp -= std::chrono::duration<int, std::ratio<3600 * 24 * 365>>(505000);
std::cout << "-505000 years: " << asString(tp) << std::endl;// underflow
}
|
沒有留言:
張貼留言