Question performance,
via TStringList, le parsing est très rapide (d'ailleurs on peut utiliser DelimitedText au lieu d'un StringReplace ... c'est plus rapide), mais les concaténations sont plus lente
via la boucle sur un raccourcissement de chaine, il est évident que le parsing ne peu être qu'affreusement lent, puisque l'on copie la chaine d'origine en la raccourcissant ...
en fin, passé la chaine en
const, fait encore gagné de la performance, car on ne passe que le pointeur sur la chaine (comme en var) sans en faire une copie ...
Voici ma contribution, ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| function SplitString(const S: string; out A: Types.TStringDynArray; Separator: Char; Limit: Cardinal): Boolean;
var
I, J: Integer;
IndexS: Integer;
LenS, LenA: Integer;
Splitted: Boolean;
begin
Result := False;
if Limit = 0 then
begin
SetLength(A, 0);
Exit;
end;
LenS := Length(S);
LenA := Ceil(LenS / Limit);
SetLength(A, LenA); // Autant alloué tout de suite, on sait que l'on aura au minimum S / Limit chaine en résultat !
IndexS := 1;
I := -1;
while IndexS < LenS do
begin
Splitted := False;
for J := IndexS + Limit downto IndexS do
begin
if (S[J] = Separator) or (LenS < J) then
begin
Inc(I);
if I > High(A) then
SetLength(A, Length(A) + 1);
A[I] := Copy(S, IndexS, J - IndexS);
IndexS := J + 1;
Splitted := True;
Break;
end;
end;
if not Splitted then
begin
SetLength(A, 0);
Exit;
end;
end;
Result := True;
end; |
Et sur la chaine
0;;1;2!2;333;44!44;55555;666!666;7777777;8888!8888;999999999;00000!00000;"AAA";"BBB";"CCC"
avec ; comme séparateur, et 12 en limite, les temps sont les suivants (P4, Ghz, 10000 fois) :
SplitString : 72 ms
RetournerListeDecoupe : 868 ms
ReturnList : 988 ms
finalement, la TStringList fonctionne pas mal !
Partager