Title: How to get execution time of a function or procedure
Question: I am optimizing a function and want to know the difference in execution time before and after optimization in microseconds!!
Answer:
put this in the var section of your function or procedure
Ctr1, Ctr2, Freq,Overhead: int64;
R: extended;
and put this at the beginnig of the function or more generally speaking at the beginning of the code you want to optimize.
QueryPerformanceFrequency(Freq);
QueryPerformanceCounter(Ctr1);
QueryPerformanceCounter(Ctr2);
Overhead := Ctr2 - Ctr1; // determine API overhead
QueryPerformanceCounter(Ctr1);
put this at the end of the code
QueryPerformanceCounter(Ctr2);
R := ((Ctr2 - Ctr1) - Overhead) / Freq;
showmessage( 'The function took ' + FloatToStr(R) + ' seconds');
important note: remember that the execution time of any function under most operating systems is affected also by services and programs running in the backgroud. So it is adviced that you keep your desktop state constant while optimizing.
This way you will get execution time in microsecond precision.
This is translated to delphi from a vb article at MSDN.