[fpc-pascal] Register variables slowing down floating point operations
cobines
cobines at gmail.com
Thu May 12 20:54:16 CEST 2011
Hello.
I have written the following program:
program a;
{$mode objfpc}
uses
SysUtils;
var
i: integer;
vd: double;
t: TDateTime;
max: int64;// = 100000000;
begin
t := Now;
max := 100000000;
for i := 0 to max do
vd := i / max;
Writeln('Time: ', DateTimeToTimeStamp(Now - t).Time, ' ms');
end.
I'm running it on Windows XP i386, compiled with FPC 2.5.1 17430.
# fpc -O a.pas
...
# a.exe
Time: 1462 ms
# fpc -O3 a.pas
...
# a.exe
Time: 3325 ms
It is slower with optimizations.
The for loop with -O looks like this:
.Lj9:
incl U_P$A_I
fildl U_P$A_I
fildq U_P$A_MAX
fdivrp %st,%st(1)
fstpl U_P$A_VD
cmpl U_P$A_I,%eax
jg .Lj9
With -O3 like this:
.Lj9:
incl %ecx
movl %ecx,-4(%ebp)
fildl -4(%ebp)
movl %edx,-16(%ebp)
movl %ebx,-12(%ebp)
fildq -16(%ebp)
fdivrp %st,%st(1)
fstpl U_P$A_VD
cmpl %ecx,%eax
jg .Lj9
It seems storing variables i and max in registers caused the code to
be slower, because they have to be written to memory anyway.
Is it something that can be rectified in the optimizer, or is it one
of those things that you just have to be aware of?
I have only found that disabling optimizations {$OPTIMIZATION OFF} ...
{$OPTIMIZATION DEFAULT} helps, but you have to do it for the entire
function.
--
cobines
More information about the fpc-pascal
mailing list