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

Florian Klaempfl F.Klaempfl at gmx.de
Mon Jun 13 21:51:43 CEST 2005


Joost van der Sluis wrote:

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

Recursive functions are expanded one level if I'am not wrong :)

> 
> 
> 
> ------------------------------------------------------------------------
> 
> 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.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel





More information about the fpc-devel mailing list