Live progress #2

Spent a few hours on refactoring the code to modern Delphi standards. The language has noticeably improved in last decade. New iterators, generics, lambdas, other fancy buzzwords. It appears though, some of them are very handy and speedier!

In the older days we would iterate over array using “for 0 to Length – 1 do” construct, which is a nice method, except when we need to have 2 nested loops with a common check iterating over an objects property (HouseArea 2D array):

for I := 1 to 4 do for K :=1 to 4 do if HouseArea[I,K] <> 0 then T := .. I + .. K

First of all it is a lot of code duplication all over the project, cos we need to check for house area on many occasions. Secondly it’s quite lengthy to write, including declaration of counters and temp variables. Thirdly – it’s slow, for each run iterator needs to query objects property.

So how modern Delphi can help us? I made a testbed program and compared several different ways of doing it. The winner is neat and fast – 3 times faster in fact (and 2 times neater):

“for T in HouseArea do // Where T is an actual point within the area”

Firstly we cache HouseArea on init with account to “<> 0” condition, secondly this iterator queries array only once per run – no need for extra pointers, thirdly we get point T immediately in each loop.

Of course that’s a synthetic test, so in-game speed might have changed by fractions of a percent. The biggest advantage is doubtless though – neater and shorter code.

This entry was posted in Live progress. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.