Re: New function's UpperCase / LowerCase



Hi John

"John O'Harrow" <john@xxxxxxxxxxxxxxxxxxxx> skrev i en meddelelse
news:456b4c5f@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi Lars,

Before testing, a couple of suggestions:-

The line:-

pResult := PChar(Result);

performs an unnecessary call of the @LStrToPChar function. This could be
replaced by:-

pResult := pointer(Result);


Thank you taking time to look at my function.

I tested you first suggestion. (UpperCase_LBG_Pas_2)

Why is that slower ? maybe mis alignment ?

UpperCase_LBG_Pas_1_a 4 117 312 429
UpperCase_LBG_Pas_1_d 0 123 348 471
UpperCase_LBG_Pas_1_c C 122 366 488
UpperCase_LBG_Pas_1_b 8 125 403 528


UpperCase_LBG_Pas_2_d 8 115 353 468
UpperCase_LBG_Pas_2_a 4 113 360 473
UpperCase_LBG_Pas_2_b 0 113 361 474
UpperCase_LBG_Pas_2_c C 119 407 526

The line:-
if (S[CharNo+a] >= 'a') and (S[CharNo+a] <= 'z') then
uses 2 conditional jumps. Using
if (S[CharNo+a] in ['a'..'z']) then
instead, only uses 1 condition jump.

UpperCase_LBG_Pas_3_d 0 119 327 446
UpperCase_LBG_Pas_3_c C 122 364 486
UpperCase_LBG_Pas_3_a 4 129 377 506
UpperCase_LBG_Pas_3_b 8 128 380 508

Again the AMD do not like the code :-(

With you suggestion we have removed

One unnecessary call and one conditional jump

And still my first function is the fastest on my AMD64 !

But the speed differens on the different alignment is smaller.

UpperCase_LBG_Pas_3 508-446 = 62

UpperCase_LBG_Pas_2 526-468 = 58

UpperCase_LBG_Pas_1 528-429 = 99

Any thought ?

Regards,
Lars G

function UpperCase_LBG_Pas_2_a(const S: string): string;
const a=1;
var
Max, CharNo : Cardinal;
pResult : PChar;
begin
Max := Length(S);
SetLength(Result, Max);
if Max <= 0 then exit;
pResult := pointer(Result);
CharNo := 0;
repeat
pResult[CharNo] := S[CharNo+a];
if (S[CharNo+a] >= 'a') and (S[CharNo+a] <= 'z') then
pResult[CharNo] := char(Ord(S[CharNo+a]) - 32);
Inc(CharNo);
until(CharNo >= Max);
end;

function UpperCase_LBG_Pas_3_a(const S: string): string;
const a=1;
var
Max, CharNo : Cardinal;
pResult : PChar;
begin
Max := Length(S);
SetLength(Result, Max);
if Max <= 0 then exit;
pResult := pointer(Result);
CharNo := 0;
repeat
pResult[CharNo] := S[CharNo+a];
if (S[CharNo+a] in ['a'..'z']) then
pResult[CharNo] := char(Ord(S[CharNo+a]) - 32);
Inc(CharNo);
until(CharNo >= Max);
end;





.


Quantcast