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

Article translations Article translations
Article ID: 306978 - View products that this article applies to.
This article was previously published under Q306978
Expand all | Collapse all

On This Page

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.

Build and Run a Demonstration Application

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and create a new Visual Basic Console Application.
  2. Replace the default code for Module1 with the following code:
    Module Module1
    
        Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Short
        Declare Function QueryPerformanceFrequency Lib "Kernel32" (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("Start Value: " & Ctr1)
                Console.WriteLine("End Value: " & Ctr2)
                QueryPerformanceFrequency(Freq)
                Console.WriteLine("QueryPerformanceCounter minimum resolution: 1/" & Freq & " seconds.")
                Console.WriteLine("100 Increment time: " & (Ctr2 - Ctr1) / Freq & " seconds.")
            Else
                Console.WriteLine("High-resolution counter not supported.")
            End If
            '
            ' Keep console window open.
            '
            Console.WriteLine()
            Console.Write("Press ENTER to finish ... ")
            Console.Read()
        End Sub
    
    End Module
    					
  3. 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 ...
    						
  4. Press ENTER to stop running the application and close the console window.

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.

REFERENCES

For more information, search for "QueryPerformanceCounter" and "QueryPerformanceFrequency" in the Visual Studio .NET online help.

For information on other timers, search for "timeGetTime," "GetTickCount," and "System.DateTime class" in the Visual Studio .NET online help.

Properties

Article ID: 306978 - Last Review: December 6, 2006 - Revision: 2.3
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005applies kbvs2005swept kbhowtomaster KB306978

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com