Microsoft KB Archive/306978

= How to use QueryPerformanceCounter to time code in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 306978

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q306978





For a Microsoft Visual C# .NET version of this article, see 306979.



For a Microsoft Visual Basic 6.0 version of this article, see 172338.

IN THIS TASK
SUMMARY
 * Build and Run a Demonstration Application
 * Troubleshooting

REFERENCES



SUMMARY
When you test code to identify performance bottlenecks, it is best to use the highest resolution timer that the system has to offer. This step-by-step article describes how to use the QueryPerformanceCounter function to time application code.

back to the top

Build and Run a Demonstration Application
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and create a new Visual Basic Console Application.  Replace the default code for Module1 with the following code: Module Module1

Declare Function QueryPerformanceCounter Lib &quot;Kernel32&quot; (ByRef X As Long) As Short Declare Function QueryPerformanceFrequency Lib &quot;Kernel32&quot; (ByRef X As Long) As Short

Sub Main Dim Ctr1, Ctr2, Freq As Long Dim Acc, I As Integer

' Times 100 increment operations by using QueryPerformanceCounter.

If QueryPerformanceCounter(Ctr1) Then  ' Begin timing. For I = 1 To 100                   ' Code is being timed. Acc += 1 Next QueryPerformanceCounter(Ctr2)      ' Finish timing. Console.WriteLine(&quot;Start Value: &quot; & Ctr1) Console.WriteLine(&quot;End Value: &quot; & Ctr2) QueryPerformanceFrequency(Freq) Console.WriteLine(&quot;QueryPerformanceCounter minimum resolution: 1/&quot; & Freq & &quot; seconds.&quot;) Console.WriteLine(&quot;100 Increment time: &quot; & (Ctr2 - Ctr1) / Freq & &quot; seconds.&quot;) Else Console.WriteLine(&quot;High-resolution counter not supported.&quot;) End If       ' ' Keep console window open. '       Console.WriteLine Console.Write(&quot;Press ENTER to finish ... &quot;) Console.Read End Sub

End Module  Save the application, and press the F5 key to compile and run the application. The console windows should display output similar to the following:

Start Value: 281060816204 End Value: 281060816269 QueryPerformanceCounter minimum resolution: 1/3579545 seconds. 100 Increment time: 1.81587324646009E-05 seconds.

Press ENTER to finish ...

 Press ENTER to stop running the application and close the console window.

back to the top

Troubleshooting

 * This function call may fail under some circumstances. Check the return value, and adjust your code to make certain that you receive valid results.
 * For best results, test the application multiple times when no other applications or server processes are running. Activities in other threads and processes can affect the percentage of time that the system spends in the target application.

back to the top

<div class="references_section">