Optimize code for speed

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Optimize code for speed

L505
What is more optimized code? The problem is.. number 1 is easier to comment the
source code.
Number 2 doesn't waste an extra variable (would it be a pointer? or a copy of
the var? if a pointer, any performance loss..or a very small one?)


1.  begin
       MyString:= Do.Something(somestring);
       StrList3.add(MyString);
     end

2.   begin
       StrList3.add(Do.Something(somestring));
     end

Other optimization questions:

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

 If you KNOW the integer is going to be short, is it better to specify a
 ShortInt?
 
 Is the default compiler setting regular strings, or ansistrings?
 
 Is a boolean faster than an integer who you just set as 0 and 1?

Let's pretend that I don't care about compatibility with older computers or
compilers, I just care about optimization in general.



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Moz
Reply | Threaded
Open this post in threaded view
|

Re: Optimize code for speed

Moz
L505 said:
> What is more optimized code?

The answer is "whatever your profiler says". Modern CPUs do so much
faffing with what the compiler gives them that the only way to really
know is to try it. Or if you're targetting specific (normally
embedded) hardware, look at the assembly language and work it out from
there.

Moz




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Optimize code for speed

Gerhard Scholz
In reply to this post by L505
----- Original Message -----
From: "L505" <[hidden email]>
To: "FPC-Pascal users discussions" <[hidden email]>
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
        StrList3.add(MyString);
      end

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

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
faster.

>  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
ShortInt?

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
speed.

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.

Gerhard


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Optimize code for speed

Gerhard Scholz
I don't know why my email has been destroyed a bit (my assumption is that
this is done by the list manager program).

Example 2 should have looked as this (now in one line):

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

Why everything after .add did vanish I don't know.

----- Original Message -----
From: "Gerhard Scholz" <[hidden email]>
To: "FPC-Pascal users discussions" <[hidden email]>
Sent: Friday, June 10, 2005 12:46 PM
Subject: Re: [fpc-pascal] Optimize code for speed


> ----- Original Message -----
> From: "L505" <[hidden email]>
> To: "FPC-Pascal users discussions" <[hidden email]>
> 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
>         StrList3.add(MyString);
>       end
>
> 2: begin
>         S               trList3.add
>       end
>
...



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal