[fpc-pascal] How to find where my app consumes CPU?

James Richters james.richters at productionautomation.net
Thu May 20 01:27:21 CEST 2021


> So now I am down to the timers...
> I am using TFPTimer timers in the scheduler to handle various things, 
> some of them are just one-shots to delay an action for some predetarmined time.

I don't know how TFpTimers work either, but when I want a timer in one of my console apps, I just do it by comparing a variable to "Now" like this:

Var
MyTimerVariable, MyTimerDelay : TDateTime;

//Start Timer
MyTimerVariable := Now + MyTimerDelay;

Then in my main loop where I also have a Sleep (), I just do:
If Now >= MyTimerVariable Then
   Do_Whatever;

I can't imagine why you would need anything more complicated than that for a general timer.  As I recall "Now" has a quite small resolution, much less than milliseconds.
This is pretty much guaranteed to not use any system resources other than the compare at whatever frequency the main loop runs at.. in fact I regulate the frequency of the main loop in a similar way..
At the end of the main loop I have a tight loop with Sleep and the comparison to Now and when I'm past the minimum time I want for the main loop, I stop doing the loop:

Repeat
   Sleep(1);
Until Now >= MyTimerVariable;

James



More information about the fpc-pascal mailing list