Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-06

Optimize code for speed

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Moz
to L505

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

Gerhard Scholz
to L505: What is more optimized code? The problem is.. number 1 is easier to comment the source…

----- Original Message -----

Earlier message 74 lines
From: "L505" To: "FPC-Pascal users discussions" 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
Gerhard Scholz
to 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.

Earlier message 35 lines
----- Original Message ----- From: "Gerhard Scholz" To: "FPC-Pascal users discussions" Sent: Friday, June 10, 2005 12:46 PM Subject: Re: [fpc-pascal] Optimize code for speed > ----- Original Message ----- > From: "L505" > To: "FPC-Pascal users discussions" > 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 > ...