Sign in Join

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

Mailing lists / lazarus / 2001-05

String parameters

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

SVAA

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

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.

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

real 0m0.904s
user 0m0.850s
sys 0m0.000s
time prb 3 10000000

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

Reply by email

Mattias Gaertner
to SVAA
SVAA wrote 8 lines
Hello: I've been browsing codetools, I've seen that string parameters are not declared as var nor const: [...] 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.

You are right.
I'm currently working on the codetools to get event-editing and automatic class completion. But it is progressing slowly due to lack of time. My current version is unstable and not useful yet, so I did not commit it. I will use more const and var.

Mattias

Reply by email

Florian Klaempfl
to SVAA: Hello: I've been browsing codetools, I've seen that string parameters are not declared as…
you wrote 15 lines
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.

I think it doesn't matter for lazarus, because lazarus is mainly using ansistrings
and ansistring parameters are only a pointer.

Reply by email