[fpc-pascal] code optimization

Paulo Costa paco at fe.up.pt
Thu Sep 23 19:50:45 CEST 2010

On 23/09/2010 18:09, stefan077 at web.de wrote:
>> Eduardo<nec556 at retena.com>  wrote:
>> Can you try optimize for size? In some cases, it reduces L2 / L3 cache
>> miss and runs faster than O3. It happens in other compilers and
>> languages too.
> I just tried it: The code gets even slightly larger and much slower (almost a factor of 2).


Try -Os3 instead of -O3

and try to reorder

  TRectangle       =  object
  width   : Integer;
  Area    : Int64;
  NextUnusedRectangle: Integer;


  TRectangle       =  object
  width   : Integer;
  NextUnusedRectangle: Integer;
  Area    : Int64;

and the var declaration:

  i, MinValley, MinValleyWidth: Integer;
  PrevBar, NextBar            : Integer;
  RectWidth                   : Integer;
  NextBarWidth                : Integer;
  NewEntry, NewEntry2         : Integer;
  MinValleyHeight             : Integer;
  MinValleyArea               : Int64;
  TotalAreaOfFittingRectangles: Int64;
  CurrentRectangle            : Integer;
  PreviousRectangle           : Integer;
  OldFirstUnusedRectangle     : Integer;
  BarCase                     : TBarCase;
  OldPrevNextRectangle        : Integer;

It seems a matter of alignment. I tested and could extract a 5% speed 
increase (XP and fpc 2.2.4). With 2.4.0 it may not work like that but is 
worth a try.

Paulo Costa

More information about the fpc-pascal mailing list