Speed and Size: Top 10 Delphi Programming

Speed and Size: Top 10 Delphi Programming Tricks- DelphiGear.cn

“Don’t worry… it’s not the (program) size that counts.”
Optimization is one of many desirable goals in software engineering, and is often antagonistic to other important goals such as stability and maintainability.

In many case the Delphi compiler will take care of the optimization. But that’s just limited to aligning the code for pipelines, and some other small tweaks to the code. There is still much to be gained by taking into account how a computer works, and adapting your algorithm to that.

Here are some of the (unsorted) tips to have in mind while developing Delphi code:

   1. Keep your code clean. This will make it easier to read and understand the code. Use meaningful variable and constant names. For example sMessage, sName for String variables, iCounter for integer variables. Constants could all be uppercase: iRATE for integer constant and sWARNING for a string constant. For components: don’t ever use the standard naming: Button1, Edit1, Combo1. The more components you have the more time it’ll take to find the one you want.

   2. Leave the {$O+} (or {$Optimization On}) compiler directive on. With this directive, Delphi compiler produces more efficient code. Sometimes, debugging is more difficult with optimization enabled: you cannot set a breakpoint on a statement if the optimizer determines that the statement serves no purpose. All optimizations performed by Delphi’s Object Pascal compiler are guaranteed not to alter the meaning of a program.

   3. You may want to compile your programs with run-time packages. This can save a lot on size when you have a lot of programs that use the same libraries. Unfortunately, when distributing your application you will need to deploy dll’s as well.

   4. Don’t create all the forms on program startup (they are created by default) because this will make your program load slower and require a lot more memory. Instead you should use dynamically created forms.

   5. Use Windows API functions instead of writing your own functions to do the same task. They are usually faster and you will save size too.

   6. When including images in your programs try to optimize them first. When including BMP files be sure that the file has the exact size you need else crop it and make sure that you don’t have a 16 color image saved in 64K color mode. When including GIF images you may optimize their color palete with special tools. When including JPEG images you may want to use image compression. Convert JPEG image that contains 256 or less colors to a GIF image – they are much smaller.

   7. When your are going to use only one or two functions or procedures from a unit and you have the unit’s source code don’t include the whole unit in the uses section but insert only the functions you are going to use into the code. This will prevent Delphi to add to executable all the routines from that unit.

   8. Instead of using non-visible components it’s usually better to include the code in your program. There’s no need in using a component that displays Windows browse directory dialog when it’s really just a few lines of code.

   9. If you have a time-critical function (important loops) you should be very careful. What seems fast on your computer may not be as fast on other PC’s. Here you should use as small code as possible and make it fast. You may want to use assembler (always comment it).

   10. Nearly all programs have to do some rudimentary calculations of some kind. Simplifying your formulas to use faster functions is a very typical optimization opportunity. Knowing how data bandwidth and calculations perform relative to each other is also very important. For example, using tables to avoid certain recalculations is often a good idea (values for Sine and Cosine). Always try to use more efficient algorithms to replace an inefficient one are: a Binary Search to replace a Linear Search.

Happy optimizing
With this tips in mind, you can greatly reduce the time and effort that goes into making quality software. I’ve only emphasized a few situations to help ease development, there are plenty others out there. Code and explore … read the Before you start optimizing Delphi code article.

You Talk Back: Esme Bedford
I enjoyed your Speed and Size: Top Tricks page, and hope I learned from it. But …
The advice “Build it first, then find where the bottlenecks *really* are, and only then optimize” is especially important for Delphi because of
a) how smart the Delphi optimization is
b) the number of people using Delphi who have come from other backgrounds where they have been taught to apply tricks which are now unnecessary (e.g. unrolling small loops)
c) the lack of realization among the less-experienced about where big gains can be made (is unrolling a small loop worth your while anyway?)