(一)调用C++高精度时钟 std::chrono::high_resolution_clock
测试usleep()的延时误差
// high_resolution_clock example
#include <stdio.h>//#include <iostream>
//#include <ctime>
//#include <ratio>
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::duration;
using std::chrono::duration_cast;
#include <unistd.h>//usleep(): microsecond-level sleep
int main ()
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i=0; i<10; ++i) {
usleep(1);
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
printf("It took me %.6F seconds...\n", time_span.count());
return 0;
}
(二)1 小时转换为分钟/秒/毫秒/微秒
#include <iostream>
#include <string>
#include <chrono>
int main()
{
std::chrono::hours hour_time = std::chrono::hours(1);
std::chrono::minutes minutes_time = std::chrono::duration_cast<std::chrono::minutes>(hour_time);
std::chrono::seconds seconds_time = std::chrono::duration_cast<std::chrono::seconds>(hour_time);
std::chrono::milliseconds milliseconds_time = std::chrono::duration_cast<std::chrono::milliseconds>(hour_time);
std::chrono::microseconds microseconds_time = std::chrono::duration_cast<std::chrono::microseconds>(hour_time);
std::cout << "1小时可转换为 \n"
<< minutes_time.count() << "分钟 \n"
<< seconds_time.count() << "秒 \n"
<< milliseconds_time.count() << "毫秒 \n"
<< microseconds_time.count() << "微秒" << std::endl;
getchar();
return 0;
}
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!