Ferry Setiawan

Thursday, June 01, 2006

Optimizing Your Delphi Application

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.

0 Comments:

Post a Comment

<< Home