简体   繁体   中英

Checking Process duration with C

I am checking process duration with C code. It works in Windows 32 bits environment, but not in Linux 64bits.

At least my process takes longer than 3 minutes, but it shows 0.062 sec. 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? Thanks.

The function clock(3) doesn't yield the elapsed time:

The value returned is the CPU time used so far as a clock_t.

As the simplest example, if your process sleeps or block for I/O, it won't use much CPU time. If you need to measure actual duration, you could use time(3) and difftime(3) or maybe clock_gettime and the like.


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.

为什么不使用time命令来进行各种时间测量?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM