Getting CPU time spent on process vs. World time spent on process problem c++

718 Views Asked by At

I am using two different ways of obtaining time clock() and getLocalTime() because I Want both the CPU time spent on my process AND the wall clock time spent on this process. Currently I do this:

printf("CPU Time: %gms \n", (((double)(finish-start)) / CLOCKS_PER_SEC)*1000.0);
printf("Clock Time: %ldms \n", (end.sec-begin.sec)*1000+(end.msec-begin.msec));

but they are both giving me the same exact result! (on something running ~30 seconds) and I know for the fact the CPU is not spending that much time on the process. Am I using the correct functions? Thanks.

3

There are 3 best solutions below

0
On

clock() isn't the right tool for portable elapsed time. Under windows, it is defined to return the elapsed wall time, on unix type systems, it returns the elapsed processor time.

I'm sure there must be a portable solution, I'm not sure where to look though. I'd hunt through boost for a start.

0
On

No really cross platform, but on Windows there are two possibilities:

Haven't used them myself, but I believe that both would give the same results

1
On

On Unix systems, CPU time can be obtained by getrusage().