[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Timing Your ProgramsIt's frequently desirable, or just fun, to put some timing code into your programs to see the effect of changes in code, or to predict how long the program would take to run to completion. For example, the Towers of Hanoi program gives you an estimate of how many years it would take to finish based on current run time and move count. There are several possible approaches. The simplest is to call the Now function when returns the current date and time like this: var start: TDateTime; ElapsedSeconds: single; ................ start:=now; {big loop here} ElapsedSeconds:=(now-start)*SecsPerDay; SecsPerDay is a predefined constant equal to 24*60*60. Since Now returns a date-time in days and fractions of days, multiplying the difference (now-start) by SecsPerDay returns a time in seconds. The DateTime field returned by Now is only updated about every 50 milliseconds, so it's not suitable for small time intervals. The best way I've found to implement accurate timing is to use QueryPerformanceCounter and QueryPerformanceFrequency procedures to access the hardware timer in Windows - typically with 1 microsecond (millionths of a second) resolution. It's easy to use, and and accurate. Beginners can get more information about these routines is to put the cursor on the routine name in Delphi and press F1 to bring up a Help screen. (This works for any Delphi component or language element by the way.) Here's some sample code that times calls to the message processing routine application.processmessages. This routine lets Windows get control to process other messages and should be included in any time consuming loops. Here we'll time a million calls in an empty loop, then a million calls in a processmessages loop and display time based on the difference in the two timer counts (i.e. the time that must have been used just for the processmessage calls). Since we're looping a million times, all we have to do is divide the count by Freq to get the time per million calls in seconds , or equivalently, the time per call in microseconds.
procedure TForm1.Button1Click(Sender: TObject); QueryPerformanceCounter(start); {Get
initial count}
to get time
in microseconds} In most cases best just to call processmessages every 1000 times or so through the loop. If you want, download sample timing code which contains 2 versions. The second (Timing2.dpr) has processmessage calls implemented. Try changing the constant callfreq from 1000 to 1024 and see if it makes a difference in run time. Can you figure out why? Also try going to Delphi menu Project - Options - Compiler/ and turning off Optimization to see the effect on run times. Here's another program to download, IntPowerDemo. It calculates integer power of a real number using 4 different techniques, brute force, recursive, powers of 2, and Delphi's IntPower function. QueryPerformanceCounter procedure is used to calculate the time for each method. (Even the slowest calculates 1.525 about one million times per second on my medium speed PC.)
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |