自用:
需要精确时间不要使用clock()函数而要使用gettimeofday()函数;
使用nanosleep()函数控制休眠时间测试得到如下结果:
clock()函数精度在1ms(vmware中2ms)且不稳定,有时上浮有时下浮;
gettimeofday()函数精度一般在100us(vmware中1ms)以内,稳定上浮;
例外:当命令休眠时间正好在1ms时,clock()可以做到10us以内的精度,但不稳定,周期性会出现1ms的上浮误差(不适用于vmware)
测试过程中发现clock和gettimeofday误差会同时爆增,说明nanosleep的睡眠时间不绝对稳定,需要作相关处理。
#include <iostream>
#include <ctime>
#include <sys/time.h>
#include "registers_input.h"
U32* time_counting;
using namespace std;
struct timespec req;
void VehicleEnvMain(void)
{
clock_t start;
clock_t end;