VB6 high-resolution timer

Stolen elsewhere… The code below times the execution of a block of code with a resolution of fractions of a millisecond.

Declare Function QueryPerformanceCounter Lib “Kernel32″ _
(X As Currency) As Boolean
Declare Function QueryPerformanceFrequency Lib “Kernel32″ _
(X As Currency) As Boolean


Private Sub Time_Addition()
    Dim total As Currency
    Dim Ctr1 As Currency, Ctr2 As Currency, Freq As Currency
    Dim A As Long, I As Long, X As Long

    QueryPerformanceFrequency Freq
    QueryPerformanceCounter Ctr1
    For X = 1 To 1000000
        For I = 1 To 100
            A = A + 1
        Next I
    Next X

    QueryPerformanceCounter Ctr2
    total = total + (Ctr2 - Ctr1)
    Debug.Print CDbl(total / Freq) * 1000 ‘ Result in ms
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *