[英]Checking Process duration with C
I am checking process duration with C code. 我正在用C代码检查进程持续时间。 It works in Windows 32 bits environment, but not in Linux 64bits.
它适用于Windows 32位环境,但不适用于Linux 64位。
At least my process takes longer than 3 minutes, but it shows 0.062 sec. 至少我的过程需要超过3分钟,但它显示0.062秒。 My code is below.
我的代码如下。
#include <stdio.h>
#include <time.h>
#include <Windows.h>
int main(void)
{
clock_t start = clock();
//....... doing something. Sorry:D
printf("%.3f sec\n", (double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}
How to fixed this code to work in 64bits Linux also? 如何修复此代码在64位Linux上工作呢? Thanks.
谢谢。
The function clock(3)
doesn't yield the elapsed time: 功能
clock(3)
不会产生经过的时间:
The value returned is the CPU time used so far as a clock_t.
返回的值是到目前为止使用的CPU时间 。
As the simplest example, if your process sleeps or block for I/O, it won't use much CPU time. 作为最简单的示例,如果您的进程为I / O休眠或阻塞,它将不会占用太多CPU时间。 If you need to measure actual duration, you could use
time(3)
and difftime(3)
or maybe clock_gettime
and the like. 如果需要测量实际持续时间,可以使用
time(3)
和difftime(3)
或者clock_gettime
等。
Side note: I don't really understand why the code appears to work in Windows, but changing to time
+ difftime
should work on both platforms. 旁注:我真的不明白为什么代码似乎在Windows中工作,但更改为
time
+ difftime
应该适用于两个平台。
为什么不使用time命令来进行各种时间测量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.