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.
Here are some procedures I made to handle checking the duration of a function. I stuck them in a unit I called
uTesting
and then just throw into the uses clause during my testing.Declaration
variables declared
Implementation
From my knowledge, the most accurate method is by using QueryPerformanceFrequency:
code:
You didn't specify your Delphi version, but Delphi XE has a TStopWatch declared in unit Diagnostics. This will allow you to measure the runtime with reasonable precision.
Use this http://delphi.about.com/od/windowsshellapi/a/delphi-high-performance-timer-tstopwatch.htm
Try Eric Grange's Sampling Profiler.
clock_gettime()
is the high solution, which is precise to nano seconds, you can also usertdsc
, which is precise to CPU cycle, and lastly you can simply usegettimeofday()
.