Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResourceString VS Const for string literals

I have a couple of thousands string literals in a Delphi application. They have been isolated in a separate file and used for localization in the past.

Now I don't need localization any more.

Is there any performance penalty in using resourcestring compared to plain constants.

Should I change those to CONST instead?

like image 299
Gad D Lord Avatar asked Mar 18 '11 08:03

Gad D Lord


1 Answers

The const string makes a call to _UStrLAsg and the resource string ends up in LoadResString.

Since the question is about speed there is nothing like doing a test.

resourcestring
    str2 = 'str2';

const
    str1 = 'str1';

function ConstStr1: string;
begin
    result := str1;
end;

function ReceStr1: string;
begin
    result := str2;
end;

function ConstStr2: string;
begin
    result := str1;
end;

function ReceStr2: string;
begin
    result := str2;
end;

procedure Test;
var
    s1, s2, s3, s4: string;
begin
    s1 := ConstStr1;
    s2 := ReceStr1;
    s3 := ConstStr2;
    s4 := ReceStr2;
end;

For the first time I used AQTime added in DelphiXE to profile this code and here is the result. The time column show Machine Cycles.

Report from AQTime

I might have done a lot of rookie mistakes profiling this but as I see it there is a difference between const and resourcestring. If the difference is noticeable for a user depends on what you do with the string. In a loop with many iterations it can matter but used to display information to the users, not so much.

like image 195
Mikael Eriksson Avatar answered Nov 09 '22 14:11

Mikael Eriksson