performancedelphioptimizationgettickcount

Calculating the speed of routines?


What would be the best and most accurate way to determine how long it took to process a routine, such as a procedure of function?

I ask because I am currently trying to optimize a few functions in my Application, when i test the changes it is hard to determine just by looking at it if there was any improvements at all. So if I could return an accurate or near accurate time it took to process a routine, I then have a more clear idea of how well, if any changes to the code have been made.

I considered using GetTickCount, but I am unsure if this would be anything near accurate?

It would be useful to have a resuable function/procedure to calculate the time of a routine, and use it something like this:

// < prepare for calcuation of code
...
ExecuteSomeCode; // < code to test
...
// < stop calcuating code and return time it took to process

I look forward to hearing some suggestions.

Thanks.

Craig.


Solution

  • From my knowledge, the most accurate method is by using QueryPerformanceFrequency:

    code:

    var
      Freq, StartCount, StopCount: Int64;
      TimingSeconds: real;
    begin
      QueryPerformanceFrequency(Freq);
      QueryPerformanceCounter(StartCount);
      // Execute process that you want to time: ...
      QueryPerformanceCounter(StopCount);
      TimingSeconds := (StopCount - StartCount) / Freq;
      // Display timing: ... 
    end;