PDA

View Full Version : Fastest timer component for Delphi



cronodragon
13-10-2005, 04:17 AM
Which is the best timer component for Delphi? The standard TTimer component isn't really fast and precise.

I used to use Carlos Barbosa Threaded Timer, it has a resolution of 1 millisecond:

http://www.carlosb.com/ :D

Crisp_N_Dry
13-10-2005, 05:04 PM
Welcome to the forums.

With regards to your problem, I can't recommend a component but I can recommend a method of attaining high resolution timings.


var
F,S,E: Int64;//Frequency / Start / End
begin
QueryPerformanceFrequency(F);
QueryPerformanceCounter(S);
Sleep(1000);//Do something interesting here rather than just go to sleep
QueryPerformanceCounter(E);
Label1.Caption:='Frequency = '+IntToStr(F);
Label2.Caption:='Start = '+IntToStr(S);
Label3.Caption:='End = '+IntToStr(E);
Label4.Caption:='End - Start = '+IntToStr(E-S);
Label5.Caption:='Elapsed Time = '+FloatToStr((E-S)/F);

Just dump this code into a TButton.OnClick event a make sure you've got some labels on the form to get an idea of what's going on. It's pretty straightforward anyway. The sleep is just for demonstration purposes. If you are wondering why it doesn't return a value of exactly 1 (It should be around 0.99...) for the elapsed time, that is because Sleep is not accurate enough to sleep for exactly one second, not because the measurements are incorrect. This is the highest resolution timing i'm aware of on Windows based PCs. It does require a Pentium equivalent or higher to work but I'd say 95% of PCs are capable nowadays anyway.

cronodragon
13-10-2005, 07:44 PM
That's cool thanks!

cronodragon
15-10-2005, 04:20 PM
Well, at the end I found this code which does the trick quite well:
http://www.swissdelphicenter.ch/torry/showcode.php?id=216

I measured the FPS and is very precise; although it can't go faster than 60fps, that's ok because the animation looks smooth. 8)