actually the test with setcapacity and then assign is mute. assign calls clear which in return sets capacity to 0. It would be better to use addstrings in that test.
I did not investigate what's happening here, but the test tells the opposite: Setting Capacity and then calling AddStrings is slower than setting Capacity and calling Assign...
Setting SortStyle to sslNone does not result in a significant advantage (because it is already set by default).
In the attachment there's a new test with new test cases and more flexibility.
depends on the run I guess, you need to increase the test time for any meaningful results to start showing, I got any thing from 105% to 125% with the assign with capacity being at 110~120%. Most of the time the difference between the two is negligible I'm runing an 100 iteration test now to see what happens, but yeah to my eyes it seems equal to assign with capacity at the moment. Considering that assing call addstrings .....
EDIT:
last post of the day. I made a small change
testname := 'Write to and load from stream';
Write(testname + ': ':LEN);
t := now;
for j:=1 to Nit do begin
L2 := TStringList.Create;
stream := TMemoryStream.Create;
try
stream.SetSize(N*Len); //Pre allocate the memory.
L1.SaveToStream(stream);
stream.Position := 0;
L2.LoadFromStream(stream);
finally
stream.Free;
L2.Free;
end;
end;
t := Now - t;
LResults.AddObject(testname, TDouble.Create(t));
WriteLn(FormatDateTime('s.zzz" s"', t));
last shot shows the results.