0

我有一个计时器函数(用于轮询),我想每 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++;
}

我究竟做错了什么?开始时间和停止时间总是一样的...

4

1 回答 1

1

clog_gettime 被驱动关闭系统时钟。tv_nsec 成员毫无意义。
分辨率将介于 60 Hz 和 4KHz 之间。

如果你想做时间戳,你应该使用 sysTimestamp 功能。
这通常使用确实提供 ns 精度的递减计数器(在 PPC 架构中)。

但是,您的系统必须配置为使用它。

于 2013-11-16T02:02:15.513 回答