[fpc-devel] Inlining recursive functions works - and it's fast too?

Joost van der Sluis joost at cnoc.nl
Mon Jun 13 15:16:43 CEST 2005


Hi all,

i'm curious what happens here. If I make a recursive funtion inlined, it
runs way faster?

Ackerman-test results:
$ ppc386 -O3p3r -dRecursiveInline ackerman.pp && cpu/bin/timeit /home/joost/src/ackerman 10
Ack(3,10): 8189
Execution took 395377 microseconds.

$ ppc386 -O3p3r ackerman.pp && cpu/bin/timeit /home/joost/src/ackerman 10
Ack(3,10): 8189
Execution took 568076 microseconds.

Am I the only one with this speed-gain, and where does it come from?

-- 
Met vriendelijke groeten,

  Joost van der Sluis
  CNOC Informatiesystemen en Netwerken
  http://www.cnoc.nl
-------------- next part --------------
program ackermann;
uses SysUtils;

function Ack(M, N : integer) : integer; {$ifdef RecursiveInline}inline;{$endif}
begin
	if M = 0 then Ack := N+1
	else if N = 0 then Ack := Ack(M-1, 1)
	else Ack := Ack(M-1, Ack(M, N-1))
End;

var NUM, a : integer;

begin
	if ParamCount = 0 then
		NUM := 1
	else
		NUM := StrToInt(ParamStr(1));

	if NUM < 1 then NUM := 1;
	a := Ack(3, NUM);
	WriteLn( 'Ack(3,' + IntToStr(NUM) + '): ' + IntToStr(a) );
end.


More information about the fpc-devel mailing list