Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create a high resolution timer Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Algorithm
Language
Delphi 2.x
Views
94
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to create a high resolution timer

Answer:

Windows is not a real time operating system so it is not really able to reliably 
achieve high accuracy timing without using a device driver. The best I have been 
able to get is a few nanoseconds using QueryPerformanceCounter. This is the 
procedure I use:

1   var
2     WaitCal: Int64;
3   
4   procedure Wait(ns: Integer);
5   var
6     Counter, Freq, WaitUntil: Int64;
7   begin
8     if QueryPerformanceCounter(Counter) then
9     begin
10      QueryPerformanceFrequency(Freq);
11      WaitUntil := Counter + WaitCal + (ns * (Freq div 1000000));
12      while Counter < WaitUntil do
13        QueryPerformanceCounter(Counter);
14    end
15    else
16      Sleep(ns div 1000);
17  end;
18  
19  //To get improved accuracy do this a little while before using Wait()
20  
21  var
22    Start, Finish: Int64;
23  
24  Application.ProcessMessages;
25  Sleep(10);
26  QueryPerformanceCounter(Start);
27  Wait(0);
28  QueryPerformanceCounter(Finish);
29  WaitCal := Start - Finish;
30  
31  {A trick I have found to increase the reliability of this on my computer is to call 
32  Wait like this:}
33  
34  Application.ProcessMessages;
35  Sleep(0);
36  DoSomething;
37  Wait(10);
38  DoSomethingElse;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC