我有一个计时器函数(用于轮询),我想每 1uS 调用一次,但我也想确保有足够的时间让其他任务运行,所以我想测量执行此函数所需的时间。为此,我认为我可以clock_gettime()
在函数的开始和结束时使用。现在我的问题是,返回的两次总是相同的,我只是不相信我的函数会在不到 1 纳秒的时间内完成——这不可能。CPU 的时钟频率为 800MHz。我的代码:
struct timespec tempTime_start;
struct timespec tempTime_stop;
static int print = 0;
if(clock_gettime(CLOCK_REALTIME, &tempTime_start))
printf("Error in clock_gettime(): %s",strerror(errno));
...
...MY FUNCTION...
int i = 0;
for (i = 0;i<100000;i++);
if(clock_gettime(CLOCK_REALTIME, &tempTime_stop))
printf("Error in clock_gettime(): %s",strerror(errno));
if (print <= 100){
printf("%dDuration: %d\n",print,(tempTime_stop.tv_nsec - tempTime_start.tv_nsec));
printf("Start: %d\n",tempTime_start.tv_nsec);
printf("Stop: %d\n",tempTime_stop.tv_nsec);
print++;
}
我究竟做错了什么?开始时间和停止时间总是一样的...