Checking Process duration with C

222 views Asked by At

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.

2

There are 2 answers

0
cnicutar On

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.

0
Ziffusion On

Why not use the time command to get the various time measurements?