none
directcast

Answers

  •  

    in terms of performance (speed), does directcast beat ctype?


    Hi Pacerier,

    This will depend on lots of things but mostly the following criteria;

    • How busy the processor or processor(s) are at the time the operation is called.
    • How much available memory there is.

     

    On my computer sometimes CTYPE is faster, sometimes it is DirectCast that wins.

     

    You can try other conversions with a variation of the following code.

    Add one Button to a Form to try this code please.

    Note the results are in terms of Timer ticks.

    1 tick is 0 . 0000001 of a second or 100 nanoseconds. 100 * 10 ^ -9 of a second.

    In case you are curious, I've read on these forums a System.Diagnostics.Stopwatch

    is more accurate than a System.Windows.Forms.Timer instance.

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Dim sw As New System.Diagnostics.Stopwatch
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Call TestSub(sender)
    
      End Sub
    
      Private Sub TestSub(ByVal anObject As Object)
    
        Dim myButton As Button
        sw.Start()
        If TypeOf anObject Is Button Then
          sw.Start()
          myButton = CType(anObject, Button)
          sw.Stop()
          MessageBox.Show("With CTYPE elapsed time = " & (sw.ElapsedTicks).ToString)
    
          sw = New System.Diagnostics.Stopwatch
    
          sw.Start()
          myButton = DirectCast(anObject, Button)
          sw.Stop()
          MessageBox.Show("With DirectCast elapsed time = " & (sw.ElapsedTicks).ToString)
        End If
    
      End Sub
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 2:52 AM
  • Hi David Streeter,

    What spec is your computer?

    It seems about 10 times faster than mine.  :-(

    Have you tried it with the DIM outside of the FOR .... NEXT loops?

    Anyway it seems DirectCast using your method is between 6 to 12 times faster on average than CType.

    You might want to try your code like this with the DIM statement for intDirectCast and intCType

    outside of the loops like this.>>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intOrig As Integer = 12345
        Dim obj As Object = intOrig
        Const intLimit_CONST As Integer = 100000000
    
        Dim intCType, intDirectCast As Integer
    
        Dim intT1 As Integer = My.Computer.Clock.TickCount
        For i As Integer = 0 To intLimit_CONST - 1
          intCType = CType(obj, Integer)
        Next i
    
        Dim intT2 As Integer = My.Computer.Clock.TickCount
        For i As Integer = 0 To intLimit_CONST - 1
          intDirectCast = DirectCast(obj, Integer)
        Next i
    
        Dim intT3 As Integer = My.Computer.Clock.TickCount
        MsgBox("CType = " & (intT2 - intT1).ToString & " ms DirectCast = " & (intT3 - intT2).ToString & " ms")
      End Sub
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 3:13 AM
  • Hi Pacerier,

     

    IMPORTANT:

    See also this thread.>>

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/ce0e01ce-bff5-4c1c-b0c7-b040a24da57e/

    one of the posts in that thread from forum user Dave Doknjas says DirectCast only works on Reference types .

     

    I have just checked this and it appears to be True.

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 3:31 AM

All replies

  • Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim intOrig As Integer = 12345
      Dim obj As Object = intOrig
      Const intLimit_CONST As Integer = 100000000
      Dim intT1 As Integer = My.Computer.Clock.TickCount
      For i As Integer = 0 To intLimit_CONST - 1
       Dim intCType As Integer = CType(obj, Integer)
      Next i
      Dim intT2 As Integer = My.Computer.Clock.TickCount
      For i As Integer = 0 To intLimit_CONST - 1
       Dim intDirectCast As Integer = DirectCast(obj, Integer)
      Next i
      Dim intT3 As Integer = My.Computer.Clock.TickCount
      MsgBox("CType = " & (intT2 - intT1).ToString & " ms DirectCast = " & (intT3 - intT2).ToString & " ms")
     End Sub
    End Class
    
    I get approx 1900 ms for CType, and 300ms for DirectCast.
    David Streeter
    Friday, January 14, 2011 2:45 AM
  • There is some discussion here:

    http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/ce0e01ce-bff5-4c1c-b0c7-b040a24da57e

    The opinion expressed there is contradicted by the MSDN documentation, which implies that, because CType is compiled inline and avoids a procedure call, it is quicker (although they don't say quicker than what).  It should be possible to create a test example that demonstrates the difference. 

    Friday, January 14, 2011 2:48 AM
  •  

    in terms of performance (speed), does directcast beat ctype?


    Hi Pacerier,

    This will depend on lots of things but mostly the following criteria;

    • How busy the processor or processor(s) are at the time the operation is called.
    • How much available memory there is.

     

    On my computer sometimes CTYPE is faster, sometimes it is DirectCast that wins.

     

    You can try other conversions with a variation of the following code.

    Add one Button to a Form to try this code please.

    Note the results are in terms of Timer ticks.

    1 tick is 0 . 0000001 of a second or 100 nanoseconds. 100 * 10 ^ -9 of a second.

    In case you are curious, I've read on these forums a System.Diagnostics.Stopwatch

    is more accurate than a System.Windows.Forms.Timer instance.

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Dim sw As New System.Diagnostics.Stopwatch
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Call TestSub(sender)
    
      End Sub
    
      Private Sub TestSub(ByVal anObject As Object)
    
        Dim myButton As Button
        sw.Start()
        If TypeOf anObject Is Button Then
          sw.Start()
          myButton = CType(anObject, Button)
          sw.Stop()
          MessageBox.Show("With CTYPE elapsed time = " & (sw.ElapsedTicks).ToString)
    
          sw = New System.Diagnostics.Stopwatch
    
          sw.Start()
          myButton = DirectCast(anObject, Button)
          sw.Stop()
          MessageBox.Show("With DirectCast elapsed time = " & (sw.ElapsedTicks).ToString)
        End If
    
      End Sub
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 2:52 AM
  • Hi David Streeter,

    What spec is your computer?

    It seems about 10 times faster than mine.  :-(

    Have you tried it with the DIM outside of the FOR .... NEXT loops?

    Anyway it seems DirectCast using your method is between 6 to 12 times faster on average than CType.

    You might want to try your code like this with the DIM statement for intDirectCast and intCType

    outside of the loops like this.>>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intOrig As Integer = 12345
        Dim obj As Object = intOrig
        Const intLimit_CONST As Integer = 100000000
    
        Dim intCType, intDirectCast As Integer
    
        Dim intT1 As Integer = My.Computer.Clock.TickCount
        For i As Integer = 0 To intLimit_CONST - 1
          intCType = CType(obj, Integer)
        Next i
    
        Dim intT2 As Integer = My.Computer.Clock.TickCount
        For i As Integer = 0 To intLimit_CONST - 1
          intDirectCast = DirectCast(obj, Integer)
        Next i
    
        Dim intT3 As Integer = My.Computer.Clock.TickCount
        MsgBox("CType = " & (intT2 - intT1).ToString & " ms DirectCast = " & (intT3 - intT2).ToString & " ms")
      End Sub
    
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 3:13 AM
  • Hi Pacerier,

     

    IMPORTANT:

    See also this thread.>>

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/ce0e01ce-bff5-4c1c-b0c7-b040a24da57e/

    one of the posts in that thread from forum user Dave Doknjas says DirectCast only works on Reference types .

     

    I have just checked this and it appears to be True.

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Friday, January 14, 2011 3:31 AM
  • heys all thanks for the help, yea i had a warning from the ide when i tried to do it on a value-type
    Friday, January 14, 2011 6:30 AM