这个解释牵强得很。我的测试报告如下:
TStringList Loop: 1000000 ElapsedTime: 594
TStringList Loop: 1000000 ElapsedTime: 500
TStringList Loop: 1000000 ElapsedTime: 547
TStringList Loop: 1000000 ElapsedTime: 437
TStringList Loop: 1000000 ElapsedTime: 469
TStringList Loop: 1000000 ElapsedTime: 437
TStringList Loop: 1000000 ElapsedTime: 406
TStringList Loop: 1000000 ElapsedTime: 500
TStringList Loop: 1000000 ElapsedTime: 485
TStringList Loop: 1000000 ElapsedTime: 437
TStringList Loop: 1000000 ElapsedTime: 438
TStringList Loop: 1000000 ElapsedTime: 516
TStringList Loop: 1000000 ElapsedTime: 469
TStringList Loop: 1000000 ElapsedTime: 531
String Loop: 1000000 ElapsedTime: 437
String Loop: 1000000 ElapsedTime: 375
String Loop: 1000000 ElapsedTime: 406
String Loop: 1000000 ElapsedTime: 344
String Loop: 1000000 ElapsedTime: 375
String Loop: 1000000 ElapsedTime: 391
String Loop: 1000000 ElapsedTime: 391
String Loop: 1000000 ElapsedTime: 375
String Loop: 1000000 ElapsedTime: 422
String Loop: 1000000 ElapsedTime: 484
String Loop: 1000000 ElapsedTime: 422
String Loop: 1000000 ElapsedTime: 422
String Loop: 1000000 ElapsedTime: 359
String Loop: 1000000 ElapsedTime: 422
测试代码如下:
const
bbq = 1000000;
bb = 'HHHHHHHHH';
procedure TForm1.Button1Click(Sender: TObject);
var
t1, t2: DWord;
L: TStringList;
i: integer;
begin
L := TStringList.Create;
t1 := GetTickCount;
for I := 1 to bbq do //TStringList
L.Add(bb);
t2 := GetTickCount;
L.Free;
Memo1.Lines.Add(Format('TStringList Loop: %d ElapsedTime: %d', [bbq, t2 - t1]));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
t1, t2: DWord;
S: string;
i: integer;
begin
t1 := GetTickCount;
for I := 1 to bbq do // String
S := S + bb + #13#10;
t2 := GetTickCount;
Memo1.Lines.Add(Format('String Loop: %d ElapsedTime: %d', [bbq, t2 - t1]));
end;
结论正好和楼主相反。我是Delphi7 ,代码优化:“关闭”。
TStringList 在使用 ADD 方法时也是随用随分配内存,TStringList 比 String 还多了一
个检查容量和检查排序等代码,不会比 String 快的。