[Lazarus] TTimer simple issue

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
When building a simple TTimer demo I cannot seem to get it working

Any idea what the problem could be if you paste this code into your form
with a memo?

var
   TimeSpent: integer;

procedure TForm2.Button1Click(Sender: TObject);
var
   i, answer: integer;
begin
   Timer1.enabled := false;
   TimeSpent := 0;
   Timer1.Enabled := true;
   Timer1.interval := 1;
   for i := 0 to 999999999 do
   begin
     answer := i * answer;
   end;

   memo1.lines.add('time spent: ' + inttostr(timespent));

end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
   inc(TimeSpent);
end;

It says
time spent: 0
Whereas the time should be a lot.

Regards,
Lars
--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
El 18/10/20 a les 19:18, Lars via lazarus ha escrit:

> When building a simple TTimer demo I cannot seem to get it working
>
> Any idea what the problem could be if you paste this code into your form
> with a memo?
>
> var
>    TimeSpent: integer;
>
> procedure TForm2.Button1Click(Sender: TObject);
> var
>    i, answer: integer;
> begin
>    Timer1.enabled := false;
>    TimeSpent := 0;
>    Timer1.Enabled := true;
>    Timer1.interval := 1;
>    for i := 0 to 999999999 do
>    begin
>      answer := i * answer;
>    end;
>
>    memo1.lines.add('time spent: ' + inttostr(timespent));
>
> end;
>
> procedure TForm2.Timer1Timer(Sender: TObject);
> begin
>    inc(TimeSpent);
> end;
>
> It says
> time spent: 0
> Whereas the time should be a lot.

No, since your loop is busy waiting the Timer1Timer method is never
fired. Events are only processed in the main gui loop. You could add an
"Application.Processmessages" inside your loop but you must know what
you are doing (e.g. you could click again button1 and that's not desirable).

Bye

--
Luca Olivetti
Wetron Automation Technology http://www.wetron.es/
Tel. +34 93 5883004 (Ext.3010)  Fax +34 93 5883007
--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
El 18/10/2020 a las 19:18, Lars via lazarus escribió:
When building a simple TTimer demo I cannot seem to get it working

Any idea what the problem could be if you paste this code into your form with a memo?

var
  TimeSpent: integer;

procedure TForm2.Button1Click(Sender: TObject);
var
  i, answer: integer;
begin
  Timer1.enabled := false;
  TimeSpent := 0;
  Timer1.Enabled := true;
  Timer1.interval := 1;
  for i := 0 to 999999999 do
  begin
    answer := i * answer;
  end;

  memo1.lines.add('time spent: ' + inttostr(timespent));

end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  inc(TimeSpent);
end;

It says
time spent: 0
Whereas the time should be a lot.

Regards,
Lars

I don't know what are you trying to do, but if you are trying to find out how long it takes certain process, you should try other approach. Timer is low precision and it is only fired by events, so you must process event's queue.

A first and bad approach:
for i := 0 to 999999999 do
begin
   answer := i * answer;
   application.processmessages; //<-- process event queue
end;
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).

EpikTimer is a component with much better precision.

https://wiki.lazarus.freepascal.org/EpikTimer


-- 
Saludos

Santiago A.

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
The problem of your code is  that the variable <answer> is not initialized.
Regards
Evgueny

вт, 27 окт. 2020 г. в 15:28, Santiago A. via lazarus <[hidden email]>:
El 18/10/2020 a las 19:18, Lars via lazarus escribió:
When building a simple TTimer demo I cannot seem to get it working

Any idea what the problem could be if you paste this code into your form with a memo?

var
  TimeSpent: integer;

procedure TForm2.Button1Click(Sender: TObject);
var
  i, answer: integer;
begin
  Timer1.enabled := false;
  TimeSpent := 0;
  Timer1.Enabled := true;
  Timer1.interval := 1;
  for i := 0 to 999999999 do
  begin
    answer := i * answer;
  end;

  memo1.lines.add('time spent: ' + inttostr(timespent));

end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  inc(TimeSpent);
end;

It says
time spent: 0
Whereas the time should be a lot.

Regards,
Lars

I don't know what are you trying to do, but if you are trying to find out how long it takes certain process, you should try other approach. Timer is low precision and it is only fired by events, so you must process event's queue.

A first and bad approach:
for i := 0 to 999999999 do
begin
   answer := i * answer;
   application.processmessages; //<-- process event queue
end;
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).

EpikTimer is a component with much better precision.

https://wiki.lazarus.freepascal.org/EpikTimer


-- 
Saludos

Santiago A.
--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
The first multiplication is when i = 0, so it should be ok. I am wondering though, the OP wants to calculate the factorial of 999999999. That would require a larger data type than an integer to store the result in! ;)

On Tue, Oct 27, 2020 at 1:46 PM Евгений Кадисов via lazarus <[hidden email]> wrote:
The problem of your code is  that the variable <answer> is not initialized.
Regards
Evgueny

вт, 27 окт. 2020 г. в 15:28, Santiago A. via lazarus <[hidden email]>:
El 18/10/2020 a las 19:18, Lars via lazarus escribió:
When building a simple TTimer demo I cannot seem to get it working

Any idea what the problem could be if you paste this code into your form with a memo?

var
  TimeSpent: integer;

procedure TForm2.Button1Click(Sender: TObject);
var
  i, answer: integer;
begin
  Timer1.enabled := false;
  TimeSpent := 0;
  Timer1.Enabled := true;
  Timer1.interval := 1;
  for i := 0 to 999999999 do
  begin
    answer := i * answer;
  end;

  memo1.lines.add('time spent: ' + inttostr(timespent));

end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  inc(TimeSpent);
end;

It says
time spent: 0
Whereas the time should be a lot.

Regards,
Lars

I don't know what are you trying to do, but if you are trying to find out how long it takes certain process, you should try other approach. Timer is low precision and it is only fired by events, so you must process event's queue.

A first and bad approach:
for i := 0 to 999999999 do
begin
   answer := i * answer;
   application.processmessages; //<-- process event queue
end;
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).

EpikTimer is a component with much better precision.

https://wiki.lazarus.freepascal.org/EpikTimer


-- 
Saludos

Santiago A.
--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] TTimer simple issue

Free Pascal - Lazarus mailing list
On Tue, 27 Oct 2020 14:40:27 +0100, Darius Blaszyk via lazarus
<[hidden email]> 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

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus