Pete L
Pete L

Reputation: 61

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

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.

Upvotes: 3

Views: 725

Answers (3)

CharlesB
CharlesB

Reputation: 90336

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

Upvotes: 1

ergosys
ergosys

Reputation: 49019

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.

Upvotes: 0

DS.
DS.

Reputation: 24110

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

Upvotes: 1

Related Questions