Results 1 to 2 of 2

Thread: Comparing code-execution times

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Comparing code-execution times

    Here is the commented approach I have used for comparing
    execution times of different pieces of code.
    I have used this very approach hundreds of times over.

    It is very accurate and easy to use.
    In this example I am only comparing two pieces of code,
    but one can use it to compare many more.

    VB Code:
    1. Option Explicit
    2.  
    3. '' This function will return the total number of milliseconds
    4. ''   since the computer was started.
    5. ''
    6. Private Declare Function GetTickCount Lib "kernel32" () As Long
    7.  
    8. Private Sub Form_Load()
    9.     '' We need a variable as a counter for our loops
    10.     ''
    11.     Dim i As Long
    12.    
    13.    
    14.     '' We also need variables to store the times taken
    15.     '' In this case we're comparing two pieces of code
    16.     ''   so we'll use two variables
    17.     ''
    18.     Dim time1 As Long, time2 As Long
    19.    
    20.    
    21.     '' The basic idea here is that we record the time
    22.     ''   before we start doing the comparison,
    23.     ''   then run the code a few times,
    24.     ''   and then check the time afterwards.
    25.     '' The time taken is the time before we start
    26.     ''   subtracted from the time we finish.
    27.     ''
    28.    
    29.     '' So record the time before we start
    30.     ''
    31.     time1 = GetTickCount
    32.    
    33.     '' Now because code fragments normally execute
    34.     ''   extremely fast, in order to actually get
    35.     ''   decent times to compare, we'll have to
    36.     ''   run the code a number of times in a row
    37.     ''
    38.     For i = 0 To 100000
    39.        
    40.         '' Call the code fragment
    41.         ''
    42.         isNumberNegative1 10
    43.     Next
    44.    
    45.     '' Now subtract the time we started at from the
    46.     ''   current time
    47.     time1 = GetTickCount - time1
    48.    
    49.    
    50.     time2 = GetTickCount
    51.     For i = 0 To 100000
    52.         isNumberNegative2 10
    53.     Next
    54.     time2 = GetTickCount - time2
    55.    
    56.     MsgBox "time1 : " & time1 & vbCrLf & _
    57.             "time2 : " & time2, vbInformation
    58.    
    59. End Sub
    60.  
    61.  
    62.  
    63. '' Here are the two code fragments we'll be testing
    64. '' They will both tell whether a number is a negative
    65. ''   number of not, but what we want to know, is which
    66. ''   one is faster.
    67. ''
    68.  
    69. Private Function isNumberNegative1(ByVal num As Long) As Boolean
    70.     isNumberNegative1 = num And -1
    71. End Function
    72.  
    73. Private Function isNumberNegative2(ByVal num As Long) As Boolean
    74.     isNumberNegative2 = num < 0
    75. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

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



Click Here to Expand Forum to Full Width