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

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

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.

Propriedades

ID do Artigo: 306978 - Última Revisão: 6 de dez de 2006 - Revisão: 1

Comentários