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

Mark Emerson ChiefAngel at angelbase.com
Wed Sep 3 01:23:02 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 := 0 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 := 0 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.

Mark




More information about the fpc-pascal mailing list