I am creating an app which mainly relies on timer,
when the timer ticks my app will do a huge task;(my task manager says 50%, in a dual core machine,one core is fully used) .
It is creating two objects and 17 variables on an event.
How can i improve the performance of my timer event ;below are my ideas ,please clarify me whether it will improve or make worse
- creating and initializing local variables globally(declare them globally)
- concurrency // is it possible to do in delphi
- using a loop with sleep function instead of timer
and i need more idas from you to decrease the timer inteval as low as possible and keep the cpu usage as low as possible
The main reason for sluggishness is nested 4 loops ; but i have no any why to remove or reduce them
//======================== updated =================================================
Thanks for the response my app will monitor webcam and alert when a motion is detected ;
but sorry i cant post my code here;but i can tell u what is happening around the code
first the image will be broken in to 25*25 pieces ;2 loops for this
average color of a piece is stored retrived and compared with the current one ; 2 loops for this
then the app will show where the motion is detected
timer interval is currently set to 300
Creating 2 class instances and 17 variables will just be done in no time – in much less time than the timer resolution is, in all cases.
So there something else in your timer which burns your CPU. Data access, processing, calculation. It is impossible to guess what does need optimization without more information and source code.
What you’ll need is to profile your application. You have AQTime in the latest versions of Delphi, but you may take a look at GPProfile or ProDelphi. You can use a profile-ready logging system like ours to make it on customer-side, if needed. Then you’ll see what is slow, i.e. where most of the time is spent.
Using a loop with a sleep (in a background thread I guess) won’t be faster than a timer.
IMHO what makes some repetitive task fast is using some pre-computed tables. There are perhaps some data that do not need to be computed or retrieved each time. So you may store those data in shared variables, and use it in the event handler. Always consider that changing an algorithm or avoiding data/hardware access is the key of speed. And always trust a profiler, not your first instinct.
After updated question
So your problem is directly related to process time of a picture. You still are in need of actual profiling to guess what shall be improved. I guess that you retrieve the picture in this timer. The process of a webcam picture won’t take 300 ms. So a possibility may be to use a background thread to retrieve the webcam images, push them in a round-robin list, then let the timer only process the latest image when asked for. But a real profiling of your code is needed.
If the picture process itself in Delphi loops is the slow part, a sampling profiler will help you see which line of code needs speed up. Some advices in this case:
for x := xRef-3 to xRef+3 do..– main rule is to avoid any branch in your code – the lessif ... thenorfor ... toappear in your code, the better;