Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetTickCout() and Milliseconds

Status
Not open for further replies.

RobS23

Programmer
Jun 4, 2001
161
GB
How do I accuratley measure milliseconds? I wish to write a VB program that displays an image and the response to that image is timed (very accurately) in milliseconds.

Any tips would be a great help
 
The GetTickCount API will give you a resolution, depending on the machine, and other activies being performed in the 4-6 millisecond range.

' Declare the API
Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long

Then inside the code, the following snippet should show its use

' Declare our Variables
Code:
Dim fLng_Start As Long
Dim fLng_End As Long
Dim fLng_Elapsed As Long

' Time Our Operation
Code:
fLng_Start = GetTickCount
<Perform the Operation>
fLng_End = GetTickCount
fLng_Elapsed = fLng_End - fLng_Start

' fLng_Elapsed contains the operation time

As stated, this will give you precision in the 4 to 6 millisecond range. If you need more than that, you can use the QueryPerformanceCounter (provided its installed), but is a little more involved. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hmm. My understanding is that GetTickCount is limited to the resolution of the system timer, which is approx 55ms in Windows 95/98/Me, and approx 10ms in NT/2000/XP rather than the 4-6ms quoted above.

Under these latter systems you can use the GetSystemTimeAdjustment API call to determine the exact accuracy
 
Your right strongm. While I was typing about the GetTickCount, I was thinking about the QPC, which is accurate down to roughly 4-6 microseconds. The GetTickCount is accurate to roughly 55 milliseconds. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Any suggestions folks - I'm searching away but I haven't found a solution as yet
 
Here is some code based on the QueryPerformanceCounter

In the declarations section

Private Declare Function QueryPerformanceCounter Lib &quot;kernel32&quot; (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib &quot;kernel32&quot; (lpFrequency As Currency) As Long

Dim fCur_Start As Currency
Dim fCur_End As Currency
Dim fCur_Freq As Currency

And within the body of your code

lLng_RetVal = QueryPerformanceFrequency(fCur_Freq)

lLng_RetVal = QueryPerformanceCounter(fCur_Start)
<Perform Your Task>
lLng_RetVal = QueryPerformanceCounter(fCur_End)
lDbl_Elapsed = (CDbl((fCur_End - fCur_Start) / fCur_Freq))
lLng_Milliseconds = lDbl_Elapsed / 1000 Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top