C++11 timer! delicious
One, foreword
In the process of writing a program, sometimes we need to test the execution time of our program statement, there are a lot of libraries to provide us to use, there has been no good cross-platform library can provide; And generally this kind of code is also by our programmer to call the system library, but often there will be insufficient accuracy and does not support cross-platform problems;
Here he comes… Here he comes. , he stepped on the colorful clouds come; “He” is the chrono library introduced in boost in C++11; He can achieve high precision clock, can do nanosecond;
Second, the chrono library
In C++11, is a time-related header file in the standard template library. All functions and class templates in this header are defined in the STD ::chrono namespace;
Here we mainly introduce two points of time and clock:
The general timer is the count from a certain point in time, and then to a certain point in time, is commonly called the time;
Time points:
template <class Clock, class Duration = typename Clock::duration>
class time_point;
Copy the code
STD ::chrono::time_point indicates a specific time
The first template parameter Clock is used to specify the Clock to be used. There are three types of clocks in the standard library:
-
System_clock: a real-time calendar clock that is currently system-wide (that is, consistent across processes)
-
Steady_clock: a dimension clock implemented by the current system whose ticks are uniform (equal in length).
-
High_resolution_clock: a high-resolution clock implemented by the current system.
The second template function argument is used to represent the unit of time (specialized STD ::chrono::duration<>)
Each point in time has a timestamp, the origin of time. The Unix timestamp used in chrono library is 00:00, January 1, 1970. So time_point is the length of time (duration) from the epoch.
Three, practice
Knowing these, we go to the beginning of the high-precision timer:
#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(); } // get the second doublegetTimerSecond()
{
returnGetTimerMicroSec () * 0.000001; } // get a millisecond doublegetTimerMilliSec()
{
returnGetTimerMicroSec () * 0.001; } // get subtle long longgetTimerMicroSec() {// The current clock minus the start clock countreturn duration_cast<microseconds>(high_resolution_clock::now() - _start).count();
}
private:
time_point<high_resolution_clock>_start;
};
#endif
Copy the code
Testing:
#include "TimerClock.hpp"
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;
}
Copy the code
Results:
Such high-precision tests can be used in our project code to calculate the time consuming of our program and optimize it.
To learn more about C++ background server, please pay attention to wechat official account: ====CPP background server development ====