我的目的是在定义的时间内执行 while
循环(例如,本例中为 90 秒)。它不必正好是 90 秒,但 1-2 秒的误差是可以接受的。为此,我确实使用了 clock()` 函数:
int main(void){
clock_t start, end;
volatile double elapsed;
start = clock();
int terminate = 1;
while(terminate)
{
end = clock();
elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000;
printf("elapsed time:%f\n",elapsed);
if(elapsed >= 90.0)
terminate = 0;
usleep(50000);
}
printf("done..\n");
return 0;
}
当我在我的笔记本电脑(x86、3.13 内核、gcc 4.8.2)上运行它时,我的秒表显示它完成了 72 秒。 (1000 是我笔记本电脑上 elapsed
秒级精度所必需的)
当我在 ARM 设备(armv5tejl、3.12 内核、gcc 4.6.3)上运行它时,完成代码需要 58 秒。 (我需要在 armv5 上的 elapsed
上使用 100
)。
我在室温下运行代码,因此时钟应该稳定。我知道内核使线程休眠并且唤醒它们的时间不准确,等等。因此,正如我之前所说,我不希望获得完美的时间,但它应该有一定的准确性。
我曾尝试仅使用 usleep
(甚至是 nanosleep
),但分辨率也不是很好。最后我想出了获取系统时间(小时、分钟、秒)然后计算耗时的底部代码。它的工作精度很高。
我想知道是否有另一种使用成本更低的解决方案?
typedef struct{
int hour;
int minute;
int second;
} timeInfo;
timeInfo getTimeInfo(void){
timeInfo value2return;
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
value2return.hour = timeinfo->tm_hour;
value2return.minute = timeinfo->tm_min;
value2return.second = timeinfo->tm_sec;
return value2return;
}
int checkElapsedTime(const timeInfo *Start, const timeInfo *Stop, const int Reference){
if(Stop->hour < Start->hour){
printf("1:%d\n", (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
if( ( (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
return 0; //while(0): terminate the loop
else
return 1; //while(1)
}else{
printf("2:%d\n",Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
if( (Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
return 0;
else
return 1;
}
}
int main(void){
timeInfo stop, start = getTimeInfo();
int terminate = 1;
while(terminate)
{
stop = getTimeInfo();
terminate = checkElapsedTime(&start, &stop, 90);
usleep(5000); //to decrease the CPU load
}
printf("terminated\n");
return 0;
}
最后,我需要在 pthread
中运行它。
最佳答案
使用
time()
与clock()
。编码目标是确定经过的墙时间而不是使用的处理器时间。当前代码计算了经过的进程时间 * 1000 并将其与 90 秒进行比较。
clock()
,@uesp 暗示,返回“时钟函数决定使用的处理器时间。” C11dr §7.27.2.1 2.time()
返回“时间函数确定当前日历时间”§7.27.2.4 2difftime()
可以很好地找到 2 个time_t
之间的差异(无论它们是什么单位/类型)并以秒为单位返回差异。int main(void) { time_t start, end; double elapsed; // seconds start = time(NULL); int terminate = 1; while (terminate) { end = time(NULL); elapsed = difftime(end, start); if (elapsed >= 90.0 /* seconds */) terminate = 0; else // No need to sleep when 90.0 seconds elapsed. usleep(50000); } printf("done..\n"); return 0; }
次要:注意:使用
clock()
时,不需要* 1000
。在运行 gcc 的基于 Windows 的机器上,对我来说,clock()
还返回了调用进程的 CPU 时间。elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000; elapsed = ((double) (end-start)) / CLOCKS_PER_SEC;
次要:不需要
volatile
。elapsed
仅由于此代码而发生变化。// volatile double elapsed; double elapsed;
关于c - 如何定义要运行几秒/分钟的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475835/