所以我正在为一个功能计时,并且遇到了一个奇怪的时间,
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
long int time = 0, itrs = 100;
struct timespec start, stop;
for (int i = 0; i < itrs; i++) {
clock_gettime(CLOCK_REALTIME, &start);
f();
clock_gettime(CLOCK_REALTIME, &stop);
time += stop.tv_nsec - start.tv_nsec;
}
printf("%ld", time/itrs);
return 0;
}
当我运行循环次数更多时,函数运行得更快。
当我运行100次时,该功能大约需要100 ns,但是当我运行1000000时它大约需要40 ns。
任何人都可以解释为什么会这样吗?