Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-09

use of funct identifier in funct body

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Emerson

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

Peter Vreman
to Mark Emerson
Show quoted text 32 lines
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.

Balázs Csaba
to Peter Vreman

If i know good, the result variable exists in function without declare it. Use result instead function name.

#Tsch : Balázs Csaba

Show quoted text 55 lines
-----Original Message----- From: On Behalf Of Peter Vreman Sent: Wednesday, September 03, 2003 8:15 AM To: Subject: Re: [fpc-pascal]use of funct identifier in funct body > 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. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Peter Vreman
to Balázs Csaba
If i know good, the result variable exists in function without declare it. Use result instead function name.

That is correct. But the result keyword is only supported in Delphi and ObjFpc modes