### Search

 Search WWW Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway,  consider using this link.

We receive a few cents from each purchase.  Thanks

### Support DFF - Donate

If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

It'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);
var
start,stop1,stop2,freq:int64;
i:integer;
begin
screen.cursor:=crHourGlass; {show busy cursor}
QueryPerformanceFrequency(freq); {Get frequency}

QueryPerformanceCounter(start); {Get initial count}

for i:=1 to 1000000 do;  {empty loop}
QueryPerformanceCounter(stop1); {Get 1st end count}
for i:=1 to 1000000 do application.processmessages; {do it loop}
QueryPerformanceCounter(stop2);  {Get 2nd end count}
screen.cursor:=crDefault;  {show normal cursor}
If freq>0  {Display (loop2 count - loop1 count)/freq

to get time in microseconds}
then showmessage('Time for a call to  processmessages is '
+ inttostr(((stop2-stop1)-(stop1-start)) div freq)
+' microseconds')
else showmessage('No hardware timer available');
end;

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.)