【发布时间】:2015-02-11 07:22:17
【问题描述】:
我试图查看使用clock_gettime 和CLOCK_MONOTONIC 的函数调用需要多少时间。我通过从函数调用的时间中减去计时器开销来做到这一点。
我认为时间应该是一致的,但是当我循环它时,第一次总是比其他时间大 3 倍。此外,我离开循环的时间也不同。有谁知道怎么回事?
(在 ubuntu 14.04.1 上运行 C)
编辑
我很困惑为什么循环中的第一次比其他的大得多。我现在意识到这可能与缓存有关。
我认为第一个循环缓存了计时器和函数? 但是当循环结束时,它们是否未缓存? (循环之后,函数调用时间变大了)
输出:
Within loop:
timer w.func difference
198 128 18446744073709551546
85 78 18446744073709551609
68 80 12
64 70 6
70 68 18446744073709551614
Outside loop:
101 115 14
我的代码:
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#define NANO 1E9
#define CLOCK CLOCK_MONOTONIC
signed long long timediff(struct timespec *tstart_p, struct timespec *tstop_p)
{
return ((tstop_p->tv_sec - tstart_p->tv_sec)*NANO + tstop_p->tv_nsec - tstart_p->tv_nsec);
}
void function()
{
}
int main()
{
struct timespec start, stop;
signed long long timepassed, timeroverhead;
printf("Within loop:\n");
printf("timer\tfunction\tdifference\n");
int i;
for (i = 0; i < 5; i++)
{
clock_gettime(CLOCK, &start);
clock_gettime(CLOCK, &stop);
timeroverhead = timediff(&start, &stop);
clock_gettime(CLOCK, &start);
function();
clock_gettime(CLOCK, &stop);
timepassed = timediff(&start, &stop);
printf("%llu\t%llu\t%llu\n", timeroverhead, timepassed, (timepassed-timeroverhead));
}
clock_gettime(CLOCK, &start);
clock_gettime(CLOCK, &stop);
timeroverhead = timediff(&start,&stop);
clock_gettime(CLOCK, &start);
function();
clock_gettime(CLOCK, &stop);
timepassed = timediff(&start, &stop);
printf("Outside loop:\n");
printf("%llu\t%llu\t%llu\n", timeroverhead, timepassed, (timepassed - timeroverhead));
printf("\n");
return 0;
}
【问题讨论】:
-
你可能想看看profiling工具
标签: c linux caching timer function-call