每次我运行这个程序时,它似乎都会在“太慢”的分支中破坏,并且在不同的时间进行。例如,它将运行超过4微秒然后突然需要25-45毫秒。任何人都知道为什么?
#include <time.h>
#include <stdio.h>
main()
{
struct timespec ts;
long currTime, prevTime, timeStep;
currTime = prevTime = timeStep = 0;
while (1)
{
clock_gettime(CLOCK_MONOTONIC, &ts);
prevTime = currTime;
currTime = ts.tv_nsec;
timeStep = currTime - prevTime;
printf("delta: %lu, currTime: %lu\n", timeStep, currTime);
if (timeStep > 16666667 && prevTime != 0)
{
printf("too slow\n");
break;
}
if (timeStep < 0 || currTime > 990000000)
{
printf("overflow (or close to it)");
break;
}
}//end while
}//end main