C++11或以上编译器,实现ms级计数,参考下面的代码
#include <chrono>
...
unsigned short _hour;
unsigned short _minute;
float _second;
...
[](unsigned short &_hour,
unsigned short &_minute,
float &_second) {
using namespace std::chrono;
auto time_now = system_clock::now();
auto duration_in_ms = duration_cast<milliseconds>(time_now.time_since_epoch());//从1970年开始
auto ms = duration_in_ms.count();//获取毫秒级时间戳 //https://www.epochconverter.com/
//auto milli = ms + 8 * 60 * 60 * 1000;//此处转化为东八区北京时间,如果是其它时区需要按需求修改
auto partial_ms = ms % 1000;//只取毫秒部份
auto tt = system_clock::to_time_t(time_now);
struct tm* ptm = localtime(&tt);
_hour = ptm->tm_hour;
_minute = ptm->tm_min;
_second = ptm->tm_sec + partial_ms / 1000.0;
}(_hour, _minute, _second);
...
C++11前,老的编译器,实现ms级计数,参考下面的代码。
#include "sperformance.h"
#include <iostream>
#include <boost/thread.hpp>
int main(int argc, char** argv)
{
kagula::PerformanceTest pt;
{
pt.start();
boost::this_thread::sleep(boost::posix_time::milliseconds(0));
pt.stop();
std::cout << "First time:" <&