-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtiming.cpp
More file actions
31 lines (24 loc) · 1.05 KB
/
timing.cpp
File metadata and controls
31 lines (24 loc) · 1.05 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
/*
We can find out the time taken by different parts of a program by using the std::chrono library.
std::chrono has two distinct objects–timepoint and duration.
A timepoint as the name suggests represents a point in time
whereas a duration represents an interval or span of time.
The C++ library allows us to subtract two timepoints to get the interval of time passed in between.
Using provided methods we can also convert this duration to appropriate units.
The std::chrono provides us with three clocks with varying accuracy.
The high_resolution_clock is the most accurate and hence it is used to measure execution time.
*/
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::literals::chrono_literals;
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(1s);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = end - start;
std::cout << duration.count() << "s" << std::endl;
std::cin.get();
return 0;
}