2013-11-15 103 views
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++; 
} 

我在做什么错?启动和停止时间总是相同...

回答

1

clog_gettime被驱动掉系统时钟。 tv_nsec成员是相当无意义的。
分辨率将在60赫兹和4K赫兹之间。

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

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