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?

blackcanopus
  • 153
  • 1
  • 4
  • 1
    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 [**documentation**](http://docwiki.embarcadero.com/Libraries/XE7/en/System.Diagnostics.TStopwatch.Start) – Tom Brunberg May 11 '18 at 09:24
  • 1
    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. – Rudy Velthuis May 11 '18 at 12:51
  • @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. – blackcanopus May 11 '18 at 15:16

1 Answers1

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);  
LU RD
  • 34,438
  • 5
  • 88
  • 296