Casting and Data Type Conversions in VB.NET
< Continued from page 1
DirectCast will usually use an Object type, so that's what I used in my first performance test. To include TryCast in the test, I also included an If block since nearly all programs that use TryCast will have one. In this case, however, it will never be executed.
Here's the code that compares all three when casting an Object to a String:
This initial test seems to show that Microsoft is right on target. Here's the result. (Experiments with larger and smaller numbers of iterations as well as repeated tests under different conditions didn't show any significant differences from this result.)
--------
Click Here to display the illustration
--------
DirectCast and TryCast were similar at 323 and 356 milliseconds, but CType took over three times as much time at 1018 milliseconds. When casting reference types like this, you pay for the flexibility of CType in performance.
But does it always work this way? The Microsoft example in their page for DirectCast is mainly useful for telling you what won't work using DirectCast, not what will. Here's the Microsoft example:
In other words, you can't use DirectCast (or TryCast, although they don't mention it here) to cast an Object type to an Integer type, but you can use DirectCast to cast a Form type to a Control type.
Let's check the performance of Microsoft's example of what will work with DirectCast. Using the same code template shown above, substitute ...
... into the code along with similar substitutions for CType and TryCast. The results are a little surprising.
--------
Click Here to display the illustration
--------
DirectCast was actually the slowest of the three choices at 145 milliseconds. CType is just a little quicker at 127 milliseconds but TryCast, including an If block, is the quickest at 77 milliseconds. I also tried writing my own objects:
I got similar results. It appears that if you're not casting an Object type, you're better off not using DirectCast.
DirectCast will usually use an Object type, so that's what I used in my first performance test. To include TryCast in the test, I also included an If block since nearly all programs that use TryCast will have one. In this case, however, it will never be executed.
Here's the code that compares all three when casting an Object to a String:
Dim theTime As New Stopwatch()Dim theString As StringDim theObject As Object = "An Object"Dim theIterations As Integer =CInt(Iterations.Text) * 1000000'' DirectCast TesttheTime.Start()For i = 0 To theIterationstheString = DirectCast(theObject, String)NexttheTime.Stop()DirectCastTime.Text =theTime.ElapsedMilliseconds.ToString'' CType TesttheTime.Restart()For i As Integer = 0 To theIterationstheString = CType(theObject, String)NexttheTime.Stop()CTypeTime.Text =theTime.ElapsedMilliseconds.ToString'' TryCast TesttheTime.Restart()For i As Integer = 0 To theIterationstheString = TryCast(theObject, String)If theString Is Nothing ThenMsgBox("This should never display")End IfNexttheTime.Stop()TryCastTime.Text =theTime.ElapsedMilliseconds.ToString
This initial test seems to show that Microsoft is right on target. Here's the result. (Experiments with larger and smaller numbers of iterations as well as repeated tests under different conditions didn't show any significant differences from this result.)
--------
Click Here to display the illustration
--------
DirectCast and TryCast were similar at 323 and 356 milliseconds, but CType took over three times as much time at 1018 milliseconds. When casting reference types like this, you pay for the flexibility of CType in performance.
But does it always work this way? The Microsoft example in their page for DirectCast is mainly useful for telling you what won't work using DirectCast, not what will. Here's the Microsoft example:
Dim q As Object = 2.37Dim i As Integer = CType(q, Integer)' The following conversion fails at run timeDim j As Integer = DirectCast(q, Integer)Dim f As New System.Windows.Forms.FormDim c As System.Windows.Forms.Control' The following conversion succeeds.c = DirectCast(f, System.Windows.Forms.Control)
In other words, you can't use DirectCast (or TryCast, although they don't mention it here) to cast an Object type to an Integer type, but you can use DirectCast to cast a Form type to a Control type.
Let's check the performance of Microsoft's example of what will work with DirectCast. Using the same code template shown above, substitute ...
c = DirectCast(f, System.Windows.Forms.Control)
... into the code along with similar substitutions for CType and TryCast. The results are a little surprising.
--------
Click Here to display the illustration
--------
DirectCast was actually the slowest of the three choices at 145 milliseconds. CType is just a little quicker at 127 milliseconds but TryCast, including an If block, is the quickest at 77 milliseconds. I also tried writing my own objects:
Class ParentClass...End ClassClass ChildClassInherits ParentClass...End Class
I got similar results. It appears that if you're not casting an Object type, you're better off not using DirectCast.
Source...