[Lazarus] TTimer simple issue

Bo Berglund bo.berglund at gmail.com
Tue Oct 27 15:21:12 CET 2020


On Tue, 27 Oct 2020 14:40:27 +0100, Darius Blaszyk via lazarus
<lazarus at lists.lazarus-ide.org> wrote:

>>> But this is very not a very efficient way. The best is to get the start
>>> time, get the end time and subtract.
>>>
>>> var
>>>   StartTime,EndTime:TDataTime;
>>>   i, answer: integer;
>>> begin
>>>  StartTime:=now;
>>>  for i := 0 to 999999999 do
>>>  begin
>>>     answer := i * answer;
>>>  end;
>>>  EndTime:=now;
>>>  memo1.lines.add('time spent: ' + TimeToStr(EndTime-StarTime) );
>>> end;
>>>
>>> But TDateTime is not accurate at all if you are measuring short periods
>>> (milliseconds).

var
  StartTime,EndTime: Int64;
  i, answer: integer;
begin
  answer := 1;
  StartTime := GetTickCount64;
  for i := 0 to 999999999 do
  begin
    answer := i * answer; //What does this really do?
  end;
  EndTime := GetTickCount64;
  memo1.lines.add('ms spent: ' + IntToStr(EndTime-StartTime));
end;

Now() is an extremely bad time source since it is really only fairly
accurate to seconds. It also uses floating point so it is rounding.
Int64 holds the same bumber of bits as a double but all of it is
dedicated to the number.


-- 
Bo Berglund
Developer in Sweden



More information about the lazarus mailing list