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 containFree
- Delphi documentation clearly states that:
TStopwatch
is not a class but still requires explicit initialization [usingStartNew
orCreate
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?
TStopWatch.Create
andTStopWatch.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 astart
and astop
command. Examples of usage ofStartNew
has been provided by others. It is worthwile to read the documentation – QuinquagesimaTStopWatch.Create
withTStopWatch.StartNew
. Now, I see I should have used the former. Thanks for the info. – Ghat