L
Lloyd Sheen
I noticed a thread a few days ago about the use of Try.. Catch versus
testing variables etc and had in my mind to test exactly what the impact
was. Its a mind boggler.
I created a small windows app that executed two subroutines.
The first simply created an object which does not implement an interface and
attempts to assign that object to an variable of the interface type. This
is done in a try ... catch block.
Private Sub UseTryCatch()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Try
inter = obj
Catch et As InvalidCastException
Dim iii As Integer = 1
Catch ex As Exception
Dim ii As Integer = 1
End Try
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeCatch.Text = interv.ToString
End Sub
The second creates the same object but uses reflection to test if the object
supports the interface and if so then the assignment would take place.
Private Sub UseReflection()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Dim typ As Type = obj.GetType
If typ.GetInterface("Tester") IsNot Nothing Then
inter = obj
End If
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeReflection.Text = interv.ToString
End Sub
The results of this test were that the try .. catch took 101087280 ticks
(about 5 seconds on a dual 3.0 G).
The reflection test took 9765 ticks (too fast to determine in my old brain).
The testing before trying was 10352 times faster. I would think that would
put to rest any questions as to the best method of capturing errors. Look
before you leap.
Hope this helps
Lloyd Sheen
testing variables etc and had in my mind to test exactly what the impact
was. Its a mind boggler.
I created a small windows app that executed two subroutines.
The first simply created an object which does not implement an interface and
attempts to assign that object to an variable of the interface type. This
is done in a try ... catch block.
Private Sub UseTryCatch()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Try
inter = obj
Catch et As InvalidCastException
Dim iii As Integer = 1
Catch ex As Exception
Dim ii As Integer = 1
End Try
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeCatch.Text = interv.ToString
End Sub
The second creates the same object but uses reflection to test if the object
supports the interface and if so then the assignment would take place.
Private Sub UseReflection()
Dim inter As Tester
Dim obj As New WithNoInterface
starttime = Now.Ticks
For i As Integer = 0 To cnt
Dim typ As Type = obj.GetType
If typ.GetInterface("Tester") IsNot Nothing Then
inter = obj
End If
Next
endtime = Now.Ticks
Dim interv As Long
interv = endtime - starttime
Me.TimeReflection.Text = interv.ToString
End Sub
The results of this test were that the try .. catch took 101087280 ticks
(about 5 seconds on a dual 3.0 G).
The reflection test took 9765 ticks (too fast to determine in my old brain).
The testing before trying was 10352 times faster. I would think that would
put to rest any questions as to the best method of capturing errors. Look
before you leap.
Hope this helps
Lloyd Sheen