去实现一下开始说的高精度计时器:
#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
测试:
#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;
}
结果: