plenderj
Apr 4th, 2002, 03:14 AM
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.
Option Explicit
'' This function will return the total number of milliseconds
'' since the computer was started.
''
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
'' We need a variable as a counter for our loops
''
Dim i As Long
'' We also need variables to store the times taken
'' In this case we're comparing two pieces of code
'' so we'll use two variables
''
Dim time1 As Long, time2 As Long
'' The basic idea here is that we record the time
'' before we start doing the comparison,
'' then run the code a few times,
'' and then check the time afterwards.
'' The time taken is the time before we start
'' subtracted from the time we finish.
''
'' So record the time before we start
''
time1 = GetTickCount
'' Now because code fragments normally execute
'' extremely fast, in order to actually get
'' decent times to compare, we'll have to
'' run the code a number of times in a row
''
For i = 0 To 100000
'' Call the code fragment
''
isNumberNegative1 10
Next
'' Now subtract the time we started at from the
'' current time
time1 = GetTickCount - time1
time2 = GetTickCount
For i = 0 To 100000
isNumberNegative2 10
Next
time2 = GetTickCount - time2
MsgBox "time1 : " & time1 & vbCrLf & _
"time2 : " & time2, vbInformation
End Sub
'' Here are the two code fragments we'll be testing
'' They will both tell whether a number is a negative
'' number of not, but what we want to know, is which
'' one is faster.
''
Private Function isNumberNegative1(ByVal num As Long) As Boolean
isNumberNegative1 = num And -1
End Function
Private Function isNumberNegative2(ByVal num As Long) As Boolean
isNumberNegative2 = num < 0
End Function
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.
Option Explicit
'' This function will return the total number of milliseconds
'' since the computer was started.
''
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
'' We need a variable as a counter for our loops
''
Dim i As Long
'' We also need variables to store the times taken
'' In this case we're comparing two pieces of code
'' so we'll use two variables
''
Dim time1 As Long, time2 As Long
'' The basic idea here is that we record the time
'' before we start doing the comparison,
'' then run the code a few times,
'' and then check the time afterwards.
'' The time taken is the time before we start
'' subtracted from the time we finish.
''
'' So record the time before we start
''
time1 = GetTickCount
'' Now because code fragments normally execute
'' extremely fast, in order to actually get
'' decent times to compare, we'll have to
'' run the code a number of times in a row
''
For i = 0 To 100000
'' Call the code fragment
''
isNumberNegative1 10
Next
'' Now subtract the time we started at from the
'' current time
time1 = GetTickCount - time1
time2 = GetTickCount
For i = 0 To 100000
isNumberNegative2 10
Next
time2 = GetTickCount - time2
MsgBox "time1 : " & time1 & vbCrLf & _
"time2 : " & time2, vbInformation
End Sub
'' Here are the two code fragments we'll be testing
'' They will both tell whether a number is a negative
'' number of not, but what we want to know, is which
'' one is faster.
''
Private Function isNumberNegative1(ByVal num As Long) As Boolean
isNumberNegative1 = num And -1
End Function
Private Function isNumberNegative2(ByVal num As Long) As Boolean
isNumberNegative2 = num < 0
End Function