# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - How to use the GetTickCount API to time a process

## MartinLiss

Did you ever wonder if one way of doing something was faster than another? If so then the following shows you how to use the GetTickCount API to time any process and find out. This is much more accurate than using a timer.


VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
      ' Place this code in any Sub or Function
    Dim lngStart As Long
    Dim lngFinish As Long
    Dim lngCounterOne As Long
    Dim lngCounterTwo As Long
    
    ' Record the start "time"
    lngStart = GetTickCount()
    
    ' Some process that you want to time
    For lngCounterOne = 1 To 1000000
        For lngCounterTwo = 1 To 5
        Next lngCounterTwo
    Next lngCounterOne
    
    ' Record the finish "time"
    
    lngFinish = GetTickCount()
    
    ' Display the difference
    MsgBox CStr(lngFinish - lngStart)

----------


## W01fgang

Thanx Martin for the code. You shaved quite a bit of time off.

But the results i got is kind of interesting.

Can someone varify this?

Add a listbox and a command button, Here's what i have done...



```
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim FastRun As Long
Dim SlowRun As Long

Private Sub Command1_Click()
Dim icount As Integer
        List1.AddItem "FAST then SLOW"
        Call CompareCountFast
        Call CompareCountSlow
        List1.AddItem "Fast: " & FastRun & "   Slow: " & SlowRun & " = Diff: " & SlowRun - FastRun & "   Factor of Diff: " & Format(SlowRun / FastRun, "0.00")
        List1.AddItem ""
        DoEvents
        List1.AddItem "SLOW then FAST"
        Call CompareCountSlow
        Call CompareCountFast
        List1.AddItem "Slow: " & SlowRun & "   Fast: " & FastRun & " = Diff: " & SlowRun - FastRun & "   Factor of Diff: " & Format(SlowRun / FastRun, "0.00")
        List1.AddItem ""
        DoEvents
    
End Sub

Private Sub CompareCountFast()
    ' Place this code in any Sub or Function
    Dim lngStart As Long
    Dim lngFinish As Long
    Dim lngCounterOne As Long
    Dim lngCounterTwo As Long
        ' Record the start "time"
    lngStart = GetTickCount()
    
    ' Some process that you want to time
    For lngCounterOne = 1 To 1000000
        For lngCounterTwo = 1 To 5
        Next 'lngCounterTwo
    Next 'lngCounterOne
    
    ' Record the finish "time"
        lngFinish = GetTickCount()
    
    ' Display the difference
    FastRun = CStr(lngFinish - lngStart)
End Sub

Private Sub CompareCountSlow()
    ' Place this code in any Sub or Function
    Dim lngStart As Long
    Dim lngFinish As Long
    Dim lngCounterOne As Long
    Dim lngCounterTwo As Long
        ' Record the start "time"
    lngStart = GetTickCount()
    
    ' Some process that you want to time
    For lngCounterOne = 1 To 5000000
        For lngCounterTwo = 1 To 5
        Next lngCounterTwo
    Next lngCounterOne
    
    ' Record the finish "time"
        lngFinish = GetTickCount()
    
    ' Display the difference
    SlowRun = CStr(lngFinish - lngStart)
End Sub
```

----------


## MartinLiss

What do you want me/us to verify? Your Slow and Fast routines seem to be different only in that one loops 1000000 while the other loops 5000000 times.

----------

