[fpc-pascal] Optimize code for speed

Gerhard Scholz gs at g--s.de
Fri Jun 10 12:46:37 CEST 2005

----- Original Message -----
From: "L505" <fpc505 at z505.com>
To: "FPC-Pascal users discussions" <fpc-pascal at lists.freepascal.org>
Sent: Friday, June 10, 2005 10:18 AM
Subject: [fpc-pascal] Optimize code for speed

> 1.  begin
>        M              yString=Do.Something
>        StrList3.add(MyString);
>      end
> 2.   begin
>                                  StrList3.add
>      end
I assume you mean:

1:   begin
        M yString=Do.Something

2: begin
        StrList3.add (do.something);

The "waste" of one little variable is not so important since when you leave
that procedure the local variables vanish. Version 2 should be a little bit

>  If you KNOW the string is going to be short, is shortstring and better,
or is
>  ansistring better?

Generally shortstring should be faster. But if you have both shortstrings
and ansistrings in your program: every conversion (e.g. by assignment) from
one to the other costs a lot. So, when you are forced to have some
ansistrings, normally it's the best to use only ansistrings.

>  If you KNOW the integer is going to be short, is it better to specify a

Depends on the machine: in i386-code longints (int32) seem to be preferred:
better handled by the compiler, and faster executed (should be at least on
pentium). On older 386 integer and shortint was better. On other processors
I don't know.

But if you have to keep a lot of data (e.g. array [ 1..1000000 ] of
an_integer_type), then space considerations might be more important than

I myself like the range constructs, e.g. var j : 1..23, when I know the
variable cannot senseful be outside that range. So debugging is done by the
program itself (with $R+,O+)

>  Is the default compiler setting regular strings, or ansistrings?

(If I understand the compiler source correctly) in delphi mode: ansistrings,
otherwise: normal strings

>  Is a boolean faster than an integer who you just set as 0 and 1?

Should be the same: a boolean is an ENUM with the values ( FALSE, TRUE )
which map to the values 0 and 1

as far as I remember that was the original definition: type boolean =
 false, true ) ;

To check what is better (e.g. your example above), you should produce a
little test program containing both of your versions and compile it with the
option "-al" to get the assembler source. Look at that source. That version
which produces less code normally should also be the faster one.


More information about the fpc-pascal mailing list