I. Introduction
In the process of writing our programs, sometimes we need to test the time-consuming execution of our program statements. There are currently many libraries available for us to use, and there has been no good cross-platform library to provide; and generally this kind of code is also It is done by our programmers who call the library of the system, but there are often problems such as insufficient precision and non-support for cross-platform; C++11 introduces the chrono library in boost; it can achieve high-precision clocks and can achieve nanosecond level ;
Two.chrono library
In chrono, the time_point template class is used to represent the time point, which supports basic arithmetic operations; different clocks return the time points of their corresponding types.
Use system_clock when you need to get the absolute time point; use steady_clock when you need to get the time interval and are not affected by system time modification.
3. Practice
Example 1:
Operation result:
Example 1 code:
#ifndef _TimerClock_hpp_
#define _TimerClock_hpp_
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
class TimerClock
{
public:
TimerClock()
{
update();
}
~TimerClock()
{
}
void update()
{
_start = high_resolution_clock::now();
}
//获取秒
double getTimerSecond()
{
return getTimerMicroSec() * 0.000001;
}
//获取毫秒
double getTimerMilliSec()
{
return getTimerMicroSec()*0.001;
}
//获取微妙
long long getTimerMicroSec()
{
//当前时钟减去开始时钟的count
return duration_cast<microseconds>(high_resolution_clock::now() - _start).count();
}
private:
time_point<high_resolution_clock>_start;
};
#endif
int main()
{
TimerClock TC;
int sum = 0;
TC.update();
for (int i = 0; i > 100000; i++)
{
sum++;
}
cout << "cost time:" << TC.getTimerMilliSec() << "ms" << endl;
cout << "cost time:" << TC.getTimerMicroSec() << "us" << endl;
return 0;
}
Four. Time processing
4.1 Classes involved
System_clock ( main concern ), steady_clock, high_resolution_clock
time_point
duration
seconds and other time representation classes
4.2 Functions involved
4.3 How to get the current time
Example 2
Example 2 code:
#include <chrono>
#include <iostream>
using namespace std;
int main()
{
// 获取当天的时间
auto curTimePoint = chrono::system_clock::now();
// 获取当前的时间戳
cout<<"获取当前的时间戳:";
cout << curTimePoint.time_since_epoch().count() << endl;
// 把当前时间向后推10秒钟
auto d1 = chrono::seconds(10);
auto time2 = curTimePoint + d1;
cout<<"把当前时间向后推10秒钟:";
cout << time2.time_since_epoch().count() << endl;
// 转为经过的小时
cout<<"转为经过的小时:";
cout << chrono::duration_cast<chrono::hours>(time2.time_since_epoch()).count() << endl;
//转为time_t的结构
cout<<"转为time_t的结构:";
time_t t = chrono::system_clock::to_time_t(time2);
cout << t << endl;
}
Five Duration
Duration is defined as:
where Rep represents the arithmetic type of counting times, which can be an integer or a decimal. For example, if it is set to double, it means that it is timed with a decimal. Period represents the included duration type, which is generally used for the conversion of different duration types, such as the different duration unit definitions given in the document:
To change the timing unit of duration, you can use duration_cast.
Example 3:
Now returns a std::chrono::time_point type. By default, duration records how many seconds are in the current time period.
Chrono provides three clocks. Generally, system_clock is used. If you want high accuracy, use high_resolution_clock
Example 3 code:
#include <iostream>
#include <chrono>
#include <thread>
#include <ratio>
using namespace std;
int main()
{
//auto start = chrono::system_clock::now();
auto start = chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(1));
//auto end = chrono::system_clock::now();
auto end = chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
chrono::duration<double, std::nano> fp_nanos = end - start;
cout<<"use "<<diff.count()<<" s\n";
cout<<"use "<<nanos.count()<<" nanos\n";
cout<<"use "<<fp_nanos.count()<<" nanos\n";
return 0;
}
Reference blog post: https://mp.weixin.qq.com/s/ZZPTOJ5qFe2lKfSWThQx-w
Reference blog post: https://mp.weixin.qq.com/s/dtEELMznaeI5cvvHOghQRA
Reference blog post: https://www.jianshu.com /p/0ccbf45491de