Using `TStopWatch` without `free`
Asked Answered
G

2

5

I am using TStopWatch for high-precision timekeeping in Delphi 10.2 Tokyo.

This website: https://www.thoughtco.com/accurately-measure-elapsed-time-1058453 has given the following example:

 var
  sw : TStopWatch;
  elapsedMilliseconds : cardinal;
begin
  sw := TStopWatch.Create() ;
  try
    sw.Start;
    //TimeOutThisFunction()
    sw.Stop;
    elapsedMilliseconds := sw.ElapsedMilliseconds;
  finally
    sw.Free;
  end;
end;

Apparently, there is a mistake there, since:

  • StopWatch does not contain Free
  • Delphi documentation clearly states that:

TStopwatch is not a class but still requires explicit initialization [using StartNew or Create methods].

This is confusing. I am using TStopWatch in a function, and I am not using free. This function may be called multiple times during each session (perhaps hundreds of times, depending on the usage). It means multiple instances of TStopWatch will be created, without being freed.

Is there a possibility of memory leaks, or other complications? If the answer is yes, what am I supposed to do? Do I have to create only one instance of TStopWatch per application? Or should I use other functions? Or something else?

Ghat answered 10/5, 2018 at 18:46 Comment(3)
The important difference between TStopWatch.Create and TStopWatch.StartNew is that the former creates the stopwatch in a stopped state, while the latter creates it in a started state. Both have their usage, f.ex. TStopWatch.Create in preparation for a repeated and cumulative timing of, say, a line in a loop, surrounded by a start and a stop command. Examples of usage of StartNew has been provided by others. It is worthwile to read the documentationQuinquagesima
Your problem is that you are confusing the TStopwatch in the linked article with the one that comes with Delphi. The former is a class, the latter a record with methods. The latter does not need a destructor or Free, and Create does not generate a new stopwatch on each call.Kape
@TomBrunberg I missed your comment somehow, but it explains a lot. I made the mistake of replacing TStopWatch.Create with TStopWatch.StartNew. Now, I see I should have used the former. Thanks for the info.Ghat
F
11

The linked example is a TStopWatch based on a class.

unit StopWatch;
interface
uses 
  Windows, SysUtils, DateUtils;

type 
  TStopWatch = class
  ...

It was published before Delphi introduced the record based TStopWatch.

Since the class variant needs to call Free after use, and the record based does not, there is no confusion here.

Just continue using the Delphi record based TStopWatch without a need to free it after use.

Usually I use the following pattern:

var
  sw : TStopWatch;
begin
  sw := TStopWatch.StartNew;
  ... // Do something
  sw.Stop;
  // Read the timing
  WriteLn(sw.ElapsedMilliseconds);  
Fenner answered 10/5, 2018 at 18:50 Comment(6)
Thanks for the clarification. What do I do with the record variant? Is it okay to use create without free?Ghat
See my update. You don't need the Create at all. It is just a record class function.Fenner
@blackcanopus: The pattern using SW.StartNew ... SW.ElapsedMilliseconds ... SW.Stop is the usual one for the TStopwatch in System.Diagnostics. No need to Create, but generally you should call StartNew.Kape
LU RD and @Rudy , there certainly is a good reason for the Create also, see my comment to OP.Quinquagesima
@Tom: I always use the StartNew...Stop sequence. Works fine.Kape
@Rudy, I didn't say it wouldn't. Just continue as it pleases you ;)Quinquagesima
V
-1

This is a class so you need free. A nice pattern is do measure time just with a memo or console, you run it till you press a key:

 sw:= TStopWatch.Create();
  try
    sw.Start;
    //TimeOutThisFunction()
    //sleep(500)
    memo2.setfocus;
    repeat until iskeypressed;
    sw.Stop;
    //elapsedMilliseconds:= sw.getValueStr; 
    writeln('stopwatch '+sw.getValueStr);
  finally
    sw.Free;
  end;
Vallery answered 19/12, 2023 at 7:35 Comment(1)
This does not answer the actual question.Immaculate

© 2022 - 2025 — McMap. All rights reserved.