the*_*rug 1 c performance system measurement infinite-loop
我想暂时运行一个无限循环.基本上,我希望有这样的东西
//do something
while(1){
//do some work
}
//do some other thing
Run Code Online (Sandbox Code Playgroud)
但我希望修复循环的运行时间,例如,循环可以运行5秒.有人有想法吗?
做sleep(5)(包括unistd.h).你可以像这样使用它:
// do some work here
someFunction();
// have a rest
sleep(5);
// do some more work
anotherFunction();
Run Code Online (Sandbox Code Playgroud)
如果你在循环中做工作,你可以做(包括time.h):
// set the end time to the current time plus 5 seconds
time_t endTime = time(NULL) + 5;
while (time(NULL) < endTime)
{
// do work here.
}
Run Code Online (Sandbox Code Playgroud)
尝试使用clock().
#include <time.h>
clock_t start = clock();
while (1)
{
clock_t now = clock();
if ((now - start)/CLOCKS_PER_SEC > 5)
break;
// Do something
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4800 次 |
| 最近记录: |