Hi.
I'm trying to make this function faster:
From the total time SetLength takes 86.1% and Move 13.6%.
On internet some sites are saying SetLength does not initialize with #0, other say it does (using FillChar).
I tested (thoroughly) and in 7/XE5 SetLength always initialize with #0.
But in this code the initialization is not required. It's just wasting time.
Is there a way to modify SetLength so it would not initialize the string with #0 and to work fine in any Delphi version...?
Or is there another function for this...?
Thank you.
Regards,
David
I'm trying to make this function faster:
Code:
//Global variables
Positions: array of Integer;
ItemSize: array of Byte;
AllStrings: AnsiString;
FPosStep = 32;
FPosStepBinPwr = 5;
function TShortStringArray.GetItem(const Idx: Integer): AnsiString;
var
i, d, Position: Integer;
begin
if (Idx < 0) or (Idx > (FCount - 1)) then
raise Exception.Create('Index out of bounds!');
d := Idx shr FPosStepBinPwr;
if ((Idx - (d shl FPosStepBinPwr)) >= (FPosStep shr 1)) and (d < High(Positions)) then
begin
Position := Positions[d + 1];
i := (d + 1) shl FPosStepBinPwr - 1;
while i >= Idx do
begin
Dec(Position, ItemSize[i]);
Dec(i);
end;
end
else
begin
Position := Positions[d];
i := d shl FPosStepBinPwr;
while i < Idx do
begin
Inc(Position, ItemSize[i]);
Inc(i);
end;
end;
if ItemSize[Idx] > 0 then
begin
SetLength(Result, ItemSize[Idx]);
Move(AllStrings[Position], Result[1], ItemSize[Idx]);
end
else
Result := '';
end;
From the total time SetLength takes 86.1% and Move 13.6%.
On internet some sites are saying SetLength does not initialize with #0, other say it does (using FillChar).
I tested (thoroughly) and in 7/XE5 SetLength always initialize with #0.
But in this code the initialization is not required. It's just wasting time.
Is there a way to modify SetLength so it would not initialize the string with #0 and to work fine in any Delphi version...?
Or is there another function for this...?
Thank you.
Regards,
David