0

I'm calculating time of method using clock() and it give me different value for the same method.

Why the result is like that ?

for(int i=0;i<10;i++){
    start = clock();
    x=FindPrimeNumber(900);
    end = clock();
    cout << end <<" "<< start  <<endl;
}

I disable the JIT optimisation using #pragma optimize( "", off ) And same thing

time: 258 time: 255 time: 253 time: 253 time: 251 time: 253 time: 251 time: 254 time: 253 time: 252

Second question how we can calculate just cpu time of a method like the clock() but just cpu time ?

And what is the quantum value of windows ? (like 40 micro second) A quantum is the time that can other process run on cpu ?

Thanks All.

Omega
  • 1,539
  • 1
  • 11
  • 18

2 Answers2

1

This is part of the indeterminism of operating systems. You could run your program under (seemingly) same conditions, and it could operate faster or slower depending on:

  • How many runnable processes are there?
  • How are the runnable processes behaving? (high IO? high CPU?)

Your process's priority can even change during execution. For example, if your process is using its entire time quantum, it will probably be moved to a lower level in the scheduling queue, meaning your program will run less often (the operating system is predicting high usage in the future).

There is no way to guarantee your process will be running on the CPU at a certain time.


My answer to your second question: instead of considering the start and end time of the method, why wouldn't you just calculate the difference between the start and the end?

int start = clock();
methodCall();
int elapsedTime = clock() - start;

Just like I said before, this will not give you the same result every time you run it. But, this should give you an approximation of how long your method is taking to complete.

One last note: when building code for a platform, whether it be Windows, Mac OSX, or Linux, you should not worry about how long your time quantum is. You're process can't detect when it has been taken off of the CPU (or at least not until it gets put back on).

The process is an abstraction provided by the operating system which allows us to not worry about the intricate details of how our process is being managed.

John
  • 3,769
  • 6
  • 30
  • 49
0

For 1st answer, I agree with John.

For 2nd answer some more details:

Clock() method gives cpu time in Linux. Visit the below answer that explains:

How can I measure CPU time and wall clock time on both Linux/Windows?

Community
  • 1
  • 1
Anurag Kanungo
  • 354
  • 3
  • 5
  • What about in windows why it's different it's act like facial time not just cpu time , Why ? – Omega Apr 18 '15 at 11:57
  • I am not sure about windows. Never tried. Though the documentation says, it gives cpu time in Windows as well. Same as for general C++. https://msdn.microsoft.com/en-us/library/aa272059%28v=vs.60%29.aspx http://www.cplusplus.com/reference/ctime/clock/ Time measures the real time while clock measures the processing time taken by the current process. – Anurag Kanungo Apr 19 '15 at 13:46