Recent

Author Topic: Question about speed  (Read 517 times)

MortenB

  • Jr. Member
  • **
  • Posts: 59
Question about speed
« on: April 12, 2021, 08:58:00 pm »
I wonder if there is any difference between procedural calls and function calls in Free Pascal.
The basic idea is that I will parse through a huge text of 300k, and record positions of various key elements in this text.
Thousands of positions will be stored in "MyResult", and I believe I should make a Pointer to this variable, and use the pointer as a procedure parameter.
But... does it really matter? Maybe the compiler does this automatically anyway?
Are there any speed preferences as to how this should be handled?

As a function:
MyResult := ParseTheText(SourceText);

As a procedural call:
ParseTheText(SourceText,MyResult);
–––
Function ParseTheText(Const SourceText:AnsiString):MyType;

OR

Procedure ParseTheText(Const SourceText:AnsiString; MyResult: MyType);

ASerge

  • Hero Member
  • *****
  • Posts: 1796
Re: Question about speed
« Reply #1 on: April 12, 2021, 09:08:52 pm »
Procedure ParseTheText(Const SourceText:AnsiString; MyResult: MyType);
I assume you mean "; var MyResult: MyType"?
Answer: no difference. For functions that have a structured or managed type result, the compiler generates a procedure, passing the result with the last parameter as var.

Sometimes a form with the "out" parameter is more descriptive.
Code: Pascal  [Select][+][-]
  1. procedure ParseTheText(const SourceText: AnsiString; out MyResult: MyType);

MortenB

  • Jr. Member
  • **
  • Posts: 59
Re: Question about speed
« Reply #2 on: April 12, 2021, 09:28:09 pm »
Thank you ASerge :)

Even a bonus. I was not aware of the "out" parameter. Great! It for sure makes it more descriptive.
But then again. I do prefer the Function, if that provides the same speed.

I just thought that doing it as a function might make two variables.
Ie... first the function fills up the "Result"-variable of the chosen type.
       Then the "Result"-variable is copied into MyResult.

My idea was to use a pointer, so that the function or procedure skips a temporary variable, and writes directly to the target variable.
As I understand you, this might be exactly what the compiler does anyway?

ASerge

  • Hero Member
  • *****
  • Posts: 1796
Re: Question about speed
« Reply #3 on: April 12, 2021, 09:45:02 pm »
I just thought that doing it as a function might make two variables.
Ie... first the function fills up the "Result"-variable of the chosen type.
       Then the "Result"-variable is copied into MyResult.
Yes, it is. So using a procedure instead of a function will allow you to do this micro-optimization. Example: use the SysUtils FmtStr function instead of the Format function.

Sometimes the functional form is safer if the variable is used incorrectly: for example, it is first modified, and then used as if without modification.
Example: this code does not work correctly in Delphi (even in version 10.3), but it works correctly in FPC.
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$IFDEF FPC}
  3.   {$MODE OBJFPC}
  4.   {$LONGSTRINGS ON}
  5. {$ENDIF}
  6.  
  7. uses
  8.   SysUtils;
  9.  
  10. var
  11.   S: string;
  12. begin
  13.   S := StringOfChar('1', 4096);
  14.   S := Format('1:%s+%:0s', [S]);
  15.   Writeln(S);
  16.   S := StringOfChar('2', 4096);
  17.   FmtStr(S, '2:%s+%:0s', [S]);
  18.   Writeln(S);
  19.   Readln;
  20. end.
« Last Edit: April 12, 2021, 09:59:18 pm by ASerge »

 

TinyPortal © 2005-2018