I suspect that if you remove the SaveToFile (which will be hiding any differences because it is by far the most expensive operation in your loop) and make it a hard loop of say a million times (or more if necessary) then you will find that the clear version is marginally faster because it avoids the overhead of creating new objects. Out of habit I tend to use the clear version. However the difference will be fairly negligable unless you have good reason to create and destroy several million objects in quick order.
 
BTW, TTime is just a double and is storing the time to a reasonable level of accuracy (but maybe not to the nearest ms). The reason ms are not being displayed is probably the TimeToStr function. Try using FormatDateTime instead, the z and zzz format specifiers will add milliseconds to your output.
 
David Brennan.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of tracey
Sent: Wednesday, 11 August 2004 10:43 AM
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Stringlist - to clear or not to clear

I would be interested to hear the opinion of all you gurus on the best practice for re-using TStringLists.

 

Scenario 1

 

Create

Use

Clear

Re-use

 

 

Scenario 2

Create

Use

Free

 

Create

Use

Free

 

I tried to test this myself by doing both ways and looping each 10000 times but both take 11 seconds….

 

procedure TForm1.TestPerformance;

var

    i: integer;

    testTimeStart, testTimeStop: TTime;

begin

 

    testTimeStart := Now;

    for i := 0 to 10000 do

    begin

        sMessageBody := TStringList.Create;

        try

            sMessagebody.Add('This is a test to see which way is better.');

            sMessageBody.Add('  This test is performed on the same TStringList but freeing it then re-creating the stringlist');

            sMessageBody.SaveToFile('test.txt');

        finally

            sMessageBody.Free;

        end;

 

    end;

    testTimeStop := Now;

    ShowMessage('create then freee= ' + TimeToStr(testTimeStop - testTimeStart));

 

    testTimeStart := Now;

    try

        sMessageBody := TStringList.Create;

        for i := 0 to 10000 do

        begin

            sMessagebody.Add('This is a test to see which way is better.');

            sMessageBody.Add('  This test is performed on the same TStringList but clearing it then re-loading the strings');

            sMessageBody.SaveToFile('test1.txt');

            sMessageBody.Clear;

        end;

    finally

        sMessageBody.Free;

        testTimeStop := Now;

        ShowMessage('create once + TimeToStr(testTimeStop - testTimeStart));

    end;

end;

 

Which brings me to Q2 - How can I get a TTime to show ms?

_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to