Here is a little tid-bit of code i found on my computer that Tests how fast your loops execute.
Create a Project.
Create a Module and add this code
Option Explicit
'API's to use
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Any) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Any) As Long
'Only three variables to use in this class
Dim Freq As Currency, TStart As Currency, TEnd As Currency
Public Property Get Frequency() As Currency
'This property only returns the clock frequency
Frequency = Freq
End Property
Private Sub Class_Initialize()
'Reset the Start and End timer variables
TStart = 0
TEnd = 0
'Return the clock frequency
QueryPerformanceFrequency Freq
If Freq <= 0 Then
'If for some reason the frequency is less than zero we are dealing with
'a high resolution clock. This is not supported with this class.
MsgBox "This timer class doesn't support high frequency clock's!"
Freq = 0
End If
End Sub
Public Sub Start()
'Return the current clock count
QueryPerformanceCounter TStart
'Reset the end result
TEnd = 0
End Sub
Public Sub StopNow()
'Return the current clock count
QueryPerformanceCounter TEnd
End Sub
Public Function Result() As Currency
'Do not return anything if the "StopNow" method is not called.
If TEnd = 0 Then
Result = -1
Exit Function
End If
'Double check to make sure the Frequency is not zero
If Freq <> 0 Then
'The time it took will equal the end time recorded, subtracting the start time
'and deviding by the frequency of the clock.
Result = (TEnd - TStart) / Freq
Else
'Return -1 if the clock frequency is to high
Result = -1
End If
End Function
Now create a form with these properties:
Property | Name | value
TextBox txtResult ""
Label Label1 Results:
CommandButton btnTest Run Timer
Form frmTimerDemo
Now add this code to the form:
Dim TTime As New cTimeIt
Private Sub btnTest_Click()
Dim iCount As Long, sFunnyResult As String
'Reset some variables
sFunnyResult = "TEST"
iFunnyResult = 0
iCount = 0
'Start the timer
TTime.Start
For iCount = 1 To 1000000
'This should take more time than the Len(x) <> 0
'This is actualy a good example for optimizing code. Always rather compare
'the lenght of a variable than to compare it with "" (string nothing). The
'len function normaly executes 20%-25% faster.
If sFunnyResult <> "" Then
iFunnyResult = iFunnyResult + 1
End If
Next
'Stop the timer
TTime.StopNow
'Return the results
txtResult = txtResult & "For Next Loop with (x <> "") took : " & TTime.Result & " sec to execute." & vbCrLf & vbCrLf
'Reset some variables
sFunnyResult = "TEST"
iFunnyResult = 0
iCount = 0
'Start the timer
TTime.Start
For iCount = 1 To 1000000
'Now compare the len of string to 0
If Len(sFunnyResult) <> 0 Then
iFunnyResult = iFunnyResult + 1
End If
Next
'Stop the timer
TTime.StopNow
'Return the results
txtResult = txtResult & "For Next Loop with (Len(x) <> 0) took : " & TTime.Result & " sec to execute." & vbCrLf & vbCrLf
End Sub
(http://members.rogers.com/codenameavalon/graphics/timer.jpg)
The commentation in the code also has some nice code optimization hints.
somehow you need to figure out the overhead of TTime.Start and TTime.Stop
Remember to boost your benchmarking thread's priority with SetThreadPriority. Additionally, for SMP testing, you might need to use SetThreadAffinityMask to control which processors it runs on.
Easy. Call Time.Start then Time.Stop.
Because all you are really interested in is a pair's overhead from the time the timer really starts until it really stops. The overhead before actual start and after actual stop, even though still in the method of Time.method, isn't relevant.