[fpc-pascal]use of funct identifier in funct body

Peter Vreman peter at freepascal.org
Wed Sep 3 08:14:57 CEST 2003


> Consider two implementations of the same function:
>
> Implementation 1 (the old way):
>
> function num_listing (n : word) : ansistring;
> var i : word;  num : string; result : ansistring;
> begin
>   result := '';
>   for i :=  to n do begin
>     str (n, num);
>     result := result + num; end;
>   num_listing := result;
> end;
>
> Implementation 2 (possible because fpc permits nonrecursive use of the
> function identifer):
>
> function num_listing (n : word) : ansistring;
> var i : word;  num : string;
> begin
>   num_listing := '';
>   for i :=  to n do begin
>     str (n, num);
>     num_listing := num_listing + num; end;
> end;
>
> Which performs better under fpc?  In other words, do we incur a
> performance penalty in #2 for the nonrecursive use of the function
> identifier, particularly in a tight loop as shown?  Or does the
> additional variable and additional assignment at the end of #1 make it
> the loser?  I know I could run a test, but I want to hear the wisdom of
> the compiler writers.  Thanks.

The #2 is faster. There is no penalty for the use of the function identifier.







More information about the fpc-pascal mailing list