QueryPerformanceCounter Function

Declare Function QueryPerformanceCounter Lib "kernel32.dll" (lpPerformanceCount As LARGE_INTEGER) As Long

Platforms

Description & Usage

QueryPerformanceCounter obtains the current reading of the system's high-performance timer. The high-performance timer is a counter that the system increments many times a second. To find out how quickly the timer is incremented, use QueryPerformanceFrequency. If you know its frequency, you can use the high-performance timer to measure small intervals of time precisely. However, keep in mind that not all computers support a high-performance timer.

Return Value

If the system contains a high-performance timer, the function returns a non-zero value. If the system does not contain such a timer, the function returns zero.

Visual Basic-Specific Issues

None.

Parameters

lpPerformanceCount
Receives the current 64-bit value of the computer's high-performance timer, if one exists.

Example

Use the high-performance timer to determine how long it takes to calculate the square roots of the first 100,000 positive integers. Run this test when the user clicks on button Command1. Obviously, to use this example, you need to place a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type LARGE_INTEGER
	LowPart As Long
	HighPart As Long
End Type
Public Declare Function QueryPerformanceFrequency Lib "kernel32.dll" (lpFrequency As LARGE_INTEGER) As Long
Public Declare Function QueryPerformanceCounter Lib "kernel32.dll" (lpPerformanceCount As LARGE_INTEGER) _
	As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
	As Any, ByVal Length As Long)

' This function converts the data in a LARGE_INTEGER structure into
' the form of VB's 64-bit Currency data type.  For more information about how this works,
' go to http://www.vbapi.com/articles/64bit/index.html.
Public Function LI2Curr (li As LARGE_INTEGER) As Currency
	Dim temp As Currency
	CopyMemory temp, li, 8
	LI2Curr = temp * 10000
End Function

' *** Place the following code inside the form. ***
Private Sub Command1_Click()
	Dim freq As Currency  ' high-performance timer frequency
	Dim count1 As Currency  ' timer reading before calculation
	Dim count2 As Currency  ' timer reading after calculation
	Dim buffer1 As LARGE_INTEGER  ' data input buffer for...
	Dim buffer2 As LARGE_INTEGER  ' ...timer functions
	Dim c As Long  ' counter variable
	Dim result As Double  ' result of square root calculation
	Dim retval As Long  ' generic return value
	
	' Get the frequency of the high-performance timer and, in the process,
	' determine if it actually exists or not.
	retval = QueryPerformanceFrequency(buffer1)
	If retval = 0 Then
		Debug.Print "This computer does not have a high-performance timer."
		Exit Sub
	End If
	freq = LI2Curr(buffer1)
	
	' Time how long it takes to calculate the first 100,000 positive integer
	' square roots.  Do this by comparing the high-performance timer reading before
	' and after the series of calculations.
	retval = QueryPerformanceCounter(buffer1)
	For c = 1 To 100000
		result = Sqr(c)
	Next c
	retval = QueryPerformanceCounter(buffer2)
	
	' Calculate the time interval between measurements.
	count1 = LI2Curr(buffer1)
	count2 = LI2Curr(buffer2)
	Debug.Print "The calculation took"; (count2 - count1) / freq; "seconds."
End Sub

See Also

QueryPerformanceFrequency

Category

Timers

Back to the Function list.
Back to the Reference section.


Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/q/queryperformancecounter.html