VB - How to use the GetTickCount API to time a process-VBForums
Results 1 to 3 of 3

Thread: VB - How to use the GetTickCount API to time a process

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,875

    VB - How to use the GetTickCount API to time a process

    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:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4.  
    5.     ' Place this code in any Sub or Function
    6.     Dim lngStart As Long
    7.     Dim lngFinish As Long
    8.     Dim lngCounterOne As Long
    9.     Dim lngCounterTwo As Long
    10.    
    11.     ' Record the start "time"
    12.     lngStart = GetTickCount()
    13.    
    14.     ' Some process that you want to time
    15.     For lngCounterOne = 1 To 1000000
    16.         For lngCounterTwo = 1 To 5
    17.         Next lngCounterTwo
    18.     Next lngCounterOne
    19.    
    20.     ' Record the finish "time"
    21.    
    22.     lngFinish = GetTickCount()
    23.    
    24.     ' Display the difference
    25.     MsgBox CStr(lngFinish - lngStart)

  2. #2
    Member
    Join Date
    Nov 2003
    Location
    Devron System
    Posts
    53
    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...

    Code:
    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

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.