Board index » delphi » How to optimize Delphi code (speed/size)?

How to optimize Delphi code (speed/size)?

Hi,

how do I optimize code written in Delphi 1/2/3 for speed and/or
size?

I am aware of various techniques, like off-screen drawing,
delayed resource allocation, and "careful" modification of (long)
strings but I now am looking for the complete picture.

This information should be as specific to Delphi and the VCL as
possible and only then consider hardware or operating system
issues.

Techniques posted here (for general discussion), or pointers to
FAQs and books (have Danny Thorpe's "Delphi component design"
already) will be greatly appreciated.

TIA!
--
Stefan.Hoffmeis...@Uni-Passau.de
http://kakadu.rz.uni-passau.de/~w4hoff01/

 

Re:How to optimize Delphi code (speed/size)?


Quote
On Sun, 29 Jun 1997, Stefan Hoffmeister wrote:
> how do I optimize code written in Delphi 1/2/3 for speed and/or
> size?

There are some simple speed-ups for Delphi (all versions):

a) Use const in parameter lists when declaring procedures. This prevents
Delphi from
creating a lokal copy and by doing this it speeds up procedure calls.

b) Use register when declaring procedures (D2 only). This forces Delphi
to use cpu registers instead of the stack, which is n times more fast.

c) Whenever possible, do a "pre-dereferenceing", that means, shorten long
references (e. g. "A.B.C.D.E.do_something;) in pre-dereferenced parts,
when the starting terms are the same (e. g. references "A.B.C.D.E.do_A"
and "A.B.C.D.E.do_B"). You can do this with the "WITH"-phrase or manually
with a lokal pointer. The fasten-up causes in the "mistake" of Delphi,
not to do a common de-referencing by itself, that means, both of the
phrases above will be coded, so the code for "A.B.C.D.E" will be
redundant, which causes speed-downs.

There are many other tricks known.

Re:How to optimize Delphi code (speed/size)?


: Markus Karg <inu94...@sophos.adv-boeblingen.de> wrote:

Quote
>On Sun, 29 Jun 1997, Stefan Hoffmeister wrote:

>> how do I optimize code written in Delphi 1/2/3 for speed and/or
>> size?

>There are some simple speed-ups for Delphi (all versions):

Thanks for following up!

Quote
>a) Use const in parameter lists when declaring procedures. This prevents
>Delphi from
>creating a lokal copy and by doing this it speeds up procedure calls.

Fine.

Same applies to VAR. Note, though that for, e.g., a TStringList
this will not yield dramatic improvements; copying a pointer is
cheap.

Quote
>b) Use register when declaring procedures (D2 only). This forces Delphi
>to use cpu registers instead of the stack, which is n times more fast.

"register" is the default calling convention which needs to be
specifically overridden with "stdcall".

Quote
>c) Whenever possible, do a "pre-dereferenceing", that means, shorten long
>references (e. g. "A.B.C.D.E.do_something;) in pre-dereferenced parts,

Delphi cannot optimize this by itself as it does not know which
side-effects this has...

Quote
>There are many other tricks known.

Which? <g>

I am looking for the whole picture, i.e. also things like

  BeginUpdate;
  try
    { lengthy operation on TStrings }
  finally
    EndUpdate;
  end;

Thanks!
--
Stefan.Hoffmeis...@Uni-Passau.de
http://kakadu.rz.uni-passau.de/~w4hoff01/

Re:How to optimize Delphi code (speed/size)?


Stefan Hoffmeister <Stefan.Hoffmeis...@Uni-Passau.de> wrote in article
<33b8d19c.1964...@news.rz.uni-passau.de>...

There are several ways to improve program speed:
1. Tricks, as in use the hardware to the max. this includes
things like those shown in the most recent Delphi Informant
to speed up graphics (BLTting 32 bits vs 16 bits; SHL & SHR
replacing multiply and divide; using the 'fast' part of the cpu,
which usually means calculating with integers; lookup tables;
etc. etc. I believe Michael Abrash is a name often mentioned
in high-speed PeeCee calculation circles - check your library
for his texts.
2. Better algorithms. Replacing the bogosort with anything else,
for example; check the texts of Jon Benson (sp?), used to write
a column on efficient programming, now has a book out that
presents edited versions of the columns. Sorry can't remember
title either. (this should be listed as item 1)
3. Avoid the calculation altogether.
4. Spend the required time, but divert the user's attention
so it seems faster. I guess this is also a 'trick'.
5. Buy a faster machine. This can be the cheapest solution.

Note that optimizing for speed / size
will pessimize cost / readability
--
Grace + Peace   *   Peter N Roth  *   Engineering Objects International
Author of "C++ Jump Start" ISBN 0-9655862-2-7.
Tools for Developers: ClassBuilder 4 for Delphi, ClassBuilder++ for C++
Visit our website at http://www.inconresearch.com/eoi

Re:How to optimize Delphi code (speed/size)?


In article <01bc859a$2aa75b40$6b92b1cd@than> "Peter N Roth" <pete*r...@erols.com (remove '*' to email me)> writes:

Quote
>There are several ways to improve program speed:
>1. Tricks, as in use the hardware to the max. this includes
>things like those shown in the most recent Delphi Informant
>to speed up graphics (BLTting 32 bits vs 16 bits; SHL & SHR
>replacing multiply and divide; using the 'fast' part of the cpu,
>which usually means calculating with integers; lookup tables;
>etc. etc. I believe Michael Abrash is a name often mentioned
>in high-speed PeeCee calculation circles - check your library
>for his texts.

Be aware also that the Delphi compiler itself is usually smart enough to do
strength-reductions like replacing multiplies with SHRs.  But it can't replace
your logic.

Quote
>2. Better algorithms. Replacing the bogosort with anything else,
>for example; check the texts of Jon Benson (sp?), used to write
>a column on efficient programming, now has a book out that
>presents edited versions of the columns. Sorry can't remember
>title either. (this should be listed as item 1)

This is the #1 "real" optimization technique.  As Kernighan and Plauger said
so long ago in "The Elements of Programming Style," "don't tweak the code...
find a better algorithm."

Quote
>3. Avoid the calculation altogether.

One related idea that can really change the time requirement is... "avoid I/O
operations whenever possible."

Quote
>4. Spend the required time, but divert the user's attention
>so it seems faster. I guess this is also a 'trick'.
>5. Buy a faster machine. This can be the cheapest solution.

Another point would be:  measure the program.  Don't assume you know where it
is actually spending its time.  Profile the thing to see where it's actually
running slow.

"Buying a faster machine" may or may not be the best answer for a developer.  
Microsoft's folks obviously use Pentium-200s with 64MB of memory... it's
obvious that they do, to all of us who don't!  :-)  I like to test software on
the slowest, pokiest, most memory-constrained machine in the house.

Re:How to optimize Delphi code (speed/size)?


Sundial Services <news-re...@sundialservices.com> wrote in article
<news-reply.5586.00C88...@sundialservices.com>...

Quote
> In article <01bc859a$2aa75b40$6b92b1cd@than> "Peter N Roth"

<pete*r...@erols.com (remove '*' to email me)> writes:
[]
Quote
> >2. Better algorithms. Replacing the bogosort with anything else,
> >for example; check the texts of Jon Benson (sp?), used to write
> >a column on efficient programming, now has a book out that

[]
I just remembered the name of the book: "Programming Pearls"
[do not expect speed from _my_ central processor!]

Quote
> >3. Avoid the calculation altogether.

> One related idea that can really change the time requirement is... "avoid
I/O
> operations whenever possible."

Or, generalizing, "avoid slow operations".
[]
Quote
> "Buying a faster machine" may or may not be the best answer for a

developer.  

Right - that's what you tell the _client_ to do.

Quote
> Microsoft's folks obviously use Pentium-200s with 64MB of memory... it's
> obvious that they do, to all of us who don't!  :-)  I like to test
software on
> the slowest, pokiest, most memory-constrained machine in the house.

You 'like' to do this? :o|
--
Grace + Peace   *   Peter N Roth  *   Engineering Objects International
Author of "C++ Jump Start" ISBN 0-9655862-2-7.
Tools for Developers: ClassBuilder 4 for Delphi, ClassBuilder++ for C++
Visit our website at http://www.inconresearch.com/eoi

Re:How to optimize Delphi code (speed/size)?


In article <33b8d19c.1964...@news.rz.uni-passau.de> Stefan.Hoffmeis...@Uni-Passau.de (Stefan Hoffmeister) writes:

Quote
>>> how do I optimize code written in Delphi 1/2/3 for speed and/or
>>> size?

>>There are some simple speed-ups for Delphi (all versions):
>Thanks for following up!
>>a) Use const in parameter lists when declaring procedures. This prevents
>>Delphi from
>>creating a lokal copy and by doing this it speeds up procedure calls.
>Fine.
>Same applies to VAR. Note, though that for, e.g., a TStringList
>this will not yield dramatic improvements; copying a pointer is
>cheap.
>>b) Use register when declaring procedures (D2 only). This forces Delphi
>>to use cpu registers instead of the stack, which is n times more fast.
>"register" is the default calling convention which needs to be
>specifically overridden with "stdcall".
>>c) Whenever possible, do a "pre-dereferenceing", that means, shorten long
>>references (e. g. "A.B.C.D.E.do_something;) in pre-dereferenced parts,
>Delphi cannot optimize this by itself as it does not know which
>side-effects this has...
>>There are many other tricks known.
>Which? <g>
>I am looking for the whole picture, i.e. also things like

All of these are good points, from an unquestionable source, but in passing
I'd also like to note that the general solution to any and all performance
problems is, "find a better algorithm."

Also, "measure the code."  Use a profiler.  See where the application is
actually spending its time.

And, "perception is everything!"  An application is "slow" only when/if you,
as a human being, perceive it to be slow.  Sometimes that is simply a matter
of response-time.  Try to observe yourself (so to speak) to see what triggers
your reaction of "it's too slow."  After all, that's why the "splash screen"
was invented.  ;-)

/mr/

Re:How to optimize Delphi code (speed/size)?


Quote
Markus Karg <inu94...@sophos.adv-boeblingen.de> wrote:
>On Sun, 29 Jun 1997, Stefan Hoffmeister wrote:
>> how do I optimize code written in Delphi 1/2/3 for speed and/or
>> size?
>There are some simple speed-ups for Delphi (all versions):
>a) Use const in parameter lists when declaring procedures. This prevents
>Delphi from
>creating a lokal copy and by doing this it speeds up procedure calls.
>b) Use register when declaring procedures (D2 only). This forces Delphi
>to use cpu registers instead of the stack, which is n times more fast.

Not necessarily. You should use register with care... it is a "hint"
to the compiler that it might want to keep some info volatile in the
registers, but just declaring everything register isn't going to
improve anything at all. On the whole, D2 does a good job of data flow
analysis.. and it's normally fairly close to what you want.

MH.

***********************************************
Martin Harvey
Uni email:
6D 63 68 32 34 40 63 61 6D 2E 61 63 2E 75 6B
Home email:
6D 63 68 32 34 40 68 61 72 76 65 79 32 37 2E 64
65 6D 6F 6E 2E 63 6F 2E 75 6B
Decode the HEX back into ASCII chars.
Uni web pages: http://www-stu.pem.cam.ac.uk/~mch24/
***********************************************

Re:How to optimize Delphi code (speed/size)?


Quote
mc...@harvey27.demon.co.uk (Martin Harvey) wrote:
>Markus Karg <inu94...@sophos.adv-boeblingen.de> wrote:
>>b) Use register when declaring procedures (D2 only). This forces Delphi
>>to use cpu registers instead of the stack, which is n times more fast.

>Not necessarily. You should use register with care... it is a "hint"
>to the compiler that it might want to keep some info volatile in the
>registers, but just declaring everything register isn't going to
>improve anything at all.

"Register" is the *default* calling convention in Delphi 2/3. The Object
Pascal Reference Guide has a chapter explaining the exact mechanics of each
calling convention, and when the stack is used and when registers are used.

In short, "register" causes the first three eligible parameters to be
passed in registers. As the first poster contested, it is more efficient.

You can't assume that it is going to be *vastly* more efficient though.
Keep in mind that the top-of-stack will almostly certainly be cached, and
given the parallelism of Pentium and higher processors, you can expect
stack based accesses to often be as efficient or nearly as efficient as
register-based ones. (I guess where you might save, is on the write back to
main memory, of the stack writes...)

--
Brad Aisa, Fact Fascist & Reason Nazi -- and damn proud of it!
web archive: http://www.interlog.com/~baisa/
email (anti-spam encoded): baisaATinterlog.com

"The highest responsibility of philosophers is to serve as the
guardians and integrators of human knowledge."   -- Ayn Rand

Re:How to optimize Delphi code (speed/size)?


Quote
Markus Karg <inu94...@sophos.adv-boeblingen.de> wrote:
> b) Use register when declaring procedures (D2 only). This forces Delphi
>    to use cpu registers instead of the stack, which is n times more fast.

Actually, "register" is the default calling convention, so there's no
need
to put this explicitly in procedural declarations.

Quote
Martin Harvey wrote:
> Not necessarily. You should use register with care... it is a "hint"
> to the compiler that it might want to keep some info volatile in the
> registers, but just declaring everything register isn't going to
> improve anything at all. On the whole, D2 does a good job of data flow
> analysis.. and it's normally fairly close to what you want.

I think you are referring to local variables, whereas "register" only
affects the function parameters. Delphi has never allowed it to be
used to affect the storage of local variables (similarly to C++).

Chris.

Re:How to optimize Delphi code (speed/size)?


One way to speed up the code and to make it smaller is to skip all
kinds of examinations like: "if Assigned( .... ) then","if
item.count>0 then".

They are needed of course but I think its wicer to use those only when
testing the software. Not anymore when software is sold to the
customer.

One way to do this easily:
-Use conditional defines (at the "options|directories/conditionals"),
for example "check_errors"
-in your code write
{$IFDEF check_errors}
if not assigned( ....) then showmessage( 'Dam, that event is never
assigned');
{$ENDIF}

When the "check_errors" is used, you will know what's wrong in your
code, and when it's removed -> the checking routine is not in your
code anymore.
**********************************************************************
"Paremmin ja paremmin"
----------------------------------------------------------------------
Janne Timmerbacka               E-mail:  janne.timmerba...@mechaul.com
Aapelinkatu 10 G 59             WWW:     None
FIN-02230 ESPOO                 Tel:     +358-9-8552801
Finland                         Fax:     Same as Tel
                                GSM:     +358-04-5525448
**********************************************************************

Re:How to optimize Delphi code (speed/size)?


: janne.timmerba...@mechaul.com (Janne Timmerbacka) wrote:

Quote
>One way to speed up the code and to make it smaller is to skip all
>kinds of examinations like: "if Assigned( .... ) then","if
>item.count>0 then".

>They are needed of course but I think its wicer to use those only when
>testing the software. Not anymore when software is sold to the
>customer.

>One way to do this easily:
>-Use conditional defines (at the "options|directories/conditionals"),
>for example "check_errors"
>-in your code write
>{$IFDEF check_errors}
>if not assigned( ....) then showmessage( 'Dam, that event is never
>assigned');
>{$ENDIF}

>When the "check_errors" is used, you will know what's wrong in your
>code, and when it's removed -> the checking routine is not in your
>code anymore.

Delphi 3 provides a nice way now for this: an "Assert" which can
be turned off.

Thanks for the hint!
--
Stefan.Hoffmeis...@Uni-Passau.de
http://kakadu.rz.uni-passau.de/~w4hoff01/

Other Threads