[lazarus] String parameters

SVAA svaa at retemail.es
Sat May 19 20:26:26 EDT 2001


Hello:

I've been browsing codetools, I've seen that string parameters are not
declared as var nor const:

procedure f(s:string);

If you are not going to modify the string in the procedure/function, it
should be declared as

procedure f(const s:string);

In the first call compiler makes a copy of the string, in the second call it
is a reference as a var parameter, but doesn't let you modify it . if the
string is a long string it is a lot of time.

look at this test calling a funtion 10000000, passing a  254 long string:

With const
real 0m0.904s
user 0m0.850s
sys 0m0.000s

With Var
real 0m0.991s
user 0m0.940s
sys 0m0.000s

Without const and without var.
real 0m17.902s
user 0m16.990s
sys 0m0.000s

This is just a 254 string, think about a string that is the whole source
file of program, and calling almost after every keystroke.

Regards
Santiago Amposta
svaa at retemail.es


This is the test:

{$Mode delphi}
program prb;
var
s:string;
i,n,Dummy:integer;

function a1(s:string):integer;
begin
a1:=ord(s[1]);
end;

function a2(const s:string):integer;
begin
a2:=ord(s[1]);
end;

function a3(var s:string):integer;
begin
a3:=ord(s[1]);
end;

begin

  s:=paramStr(1);
  for i:=2 to 254 do s:=s+'0';

  val(paramstr(2),n,i);


  for i:=1 to n do
    case s[1] of
      '1':dummy:=a1(s);
      '2':dummy:=a2(s);
      '3':dummy:=a3(s);
    end;
end.


lazarus@~$ time prb 1 10000000
real 0m17.902s
user 0m16.990s
sys 0m0.000s
lazarus@~$ time prb 2 10000000

real 0m0.904s
user 0m0.850s
sys 0m0.000s
lazarus@~$ time prb 3 10000000

real 0m0.991s
user 0m0.940s
sys 0m0.000s
lazarus@~$








More information about the Lazarus mailing list