-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.cpp
42 lines (34 loc) · 844 Bytes
/
timing.cpp
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
// troubleshooting: use -pthread flag when compiling
#include <chrono>
#include <iostream>
#include <thread>
struct Timer
{
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
std::chrono::microseconds duration;
Timer()
{
start = std::chrono::high_resolution_clock::now();
duration = std::chrono::microseconds::zero();
}
~Timer()
{
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
Print();
}
void Print()
{
std::cout << "Timer took " << duration.count() << "μs." << std::endl;
}
};
void TimedFunction()
{
Timer timer;
for (int i = 0; i < 100; i++)
std::cout << "Hello, timer!" << std::endl;
}
int main()
{
TimedFunction();
}