speed diffrencets CType and directcast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Henry

    speed diffrencets CType and directcast

    is there any speed diffrences between doing Ctype or directcast? I know
    about the inherite diffrences, but process usage time wise, does one take up
    more cycles then the other? thanks


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: speed diffrencets CType and directcast

    Brian,
    When CType is simply casting I would not expect there to be any speed
    difference.

    However when CType is converting, then I would expect the process of
    converting from one type to a different type to have more overhead then
    simply casting. However I question if the difference in speed is worth the
    bother.

    However! I find its better to write "correct" programs first, then worry
    about optimizations after a specific routine has been identified, via
    profiling, to have a performance issue.

    As you know, CType is the conversion operator used to convert one type into
    a different type, such as a String to an Integer. While DirectCast is used
    for casting, normally from a base type to a derived type, where the base
    object is of the derived type. Where Type can be classes, structures or
    interfaces...

    Note with overloading the CType operator available in Whidbey (VS.NET 2005
    due out next year) it will be even more important to use the "correct"
    operator. As a CType that fails in VB.NET 2003 may suddenly work in VB.NET
    2005...

    Of course "correct-ness" is subjective ;-)

    Hope this helps
    Jay

    "Brian Henry" <brianiupmsdn@n ewsgroups.nospa m> wrote in message
    news:eU52NSEPEH A.2960@TK2MSFTN GP10.phx.gbl...[color=blue]
    > is there any speed diffrences between doing Ctype or directcast? I know
    > about the inherite diffrences, but process usage time wise, does one take[/color]
    up[color=blue]
    > more cycles then the other? thanks
    >
    >[/color]


    Comment

    • Howard Kaikow

      #3
      Re: speed diffrencets CType and directcast

      I read something recently, I forget where, that stated DirectCast is faster,
      but cannot be used in all circumstances.

      --
      http://www.standards.com/; See Howard Kaikow's web site.
      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
      news:%23ZDTq8EP EHA.736@tk2msft ngp13.phx.gbl.. .[color=blue]
      > Brian,
      > When CType is simply casting I would not expect there to be any speed
      > difference.
      >
      > However when CType is converting, then I would expect the process of
      > converting from one type to a different type to have more overhead then
      > simply casting. However I question if the difference in speed is worth the
      > bother.
      >
      > However! I find its better to write "correct" programs first, then worry
      > about optimizations after a specific routine has been identified, via
      > profiling, to have a performance issue.
      >
      > As you know, CType is the conversion operator used to convert one type[/color]
      into[color=blue]
      > a different type, such as a String to an Integer. While DirectCast is used
      > for casting, normally from a base type to a derived type, where the base
      > object is of the derived type. Where Type can be classes, structures or
      > interfaces...
      >
      > Note with overloading the CType operator available in Whidbey (VS.NET 2005
      > due out next year) it will be even more important to use the "correct"
      > operator. As a CType that fails in VB.NET 2003 may suddenly work in VB.NET
      > 2005...
      >
      > Of course "correct-ness" is subjective ;-)
      >
      > Hope this helps
      > Jay
      >
      > "Brian Henry" <brianiupmsdn@n ewsgroups.nospa m> wrote in message
      > news:eU52NSEPEH A.2960@TK2MSFTN GP10.phx.gbl...[color=green]
      > > is there any speed diffrences between doing Ctype or directcast? I know
      > > about the inherite diffrences, but process usage time wise, does one[/color][/color]
      take[color=blue]
      > up[color=green]
      > > more cycles then the other? thanks
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: speed diffrencets CType and directcast

        Howard,
        I believe I stated that in there someplace ;-)

        My understanding is that DirectCast will only cast so it is faster, while
        CType may convert and this extra check will slow it down.

        Realizing that with Option Strict On, the compiler would/should know if
        CType needs to cast or convert, so a number of times CType & DirectCast will
        be the same speed (produce the same IL code).

        However! I don't think it really matters which is faster! IMHO What really
        matters is which is correct to use for what you are doing! As you stated,
        DirectCast cannot be used in all circumstances, basically it cannot be used
        where you need to Convert a value & not cast it!

        Hope this helps
        Jay

        "Howard Kaikow" <kaikow@standar ds.com> wrote in message
        news:eejP%23ZFP EHA.640@TK2MSFT NGP12.phx.gbl.. .[color=blue]
        > I read something recently, I forget where, that stated DirectCast is[/color]
        faster,[color=blue]
        > but cannot be used in all circumstances.
        >
        > --
        > http://www.standards.com/; See Howard Kaikow's web site.
        > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
        > news:%23ZDTq8EP EHA.736@tk2msft ngp13.phx.gbl.. .[color=green]
        > > Brian,
        > > When CType is simply casting I would not expect there to be any speed
        > > difference.
        > >
        > > However when CType is converting, then I would expect the process of
        > > converting from one type to a different type to have more overhead then
        > > simply casting. However I question if the difference in speed is worth[/color][/color]
        the[color=blue][color=green]
        > > bother.
        > >
        > > However! I find its better to write "correct" programs first, then worry
        > > about optimizations after a specific routine has been identified, via
        > > profiling, to have a performance issue.
        > >
        > > As you know, CType is the conversion operator used to convert one type[/color]
        > into[color=green]
        > > a different type, such as a String to an Integer. While DirectCast is[/color][/color]
        used[color=blue][color=green]
        > > for casting, normally from a base type to a derived type, where the base
        > > object is of the derived type. Where Type can be classes, structures or
        > > interfaces...
        > >
        > > Note with overloading the CType operator available in Whidbey (VS.NET[/color][/color]
        2005[color=blue][color=green]
        > > due out next year) it will be even more important to use the "correct"
        > > operator. As a CType that fails in VB.NET 2003 may suddenly work in[/color][/color]
        VB.NET[color=blue][color=green]
        > > 2005...
        > >
        > > Of course "correct-ness" is subjective ;-)
        > >
        > > Hope this helps
        > > Jay
        > >
        > > "Brian Henry" <brianiupmsdn@n ewsgroups.nospa m> wrote in message
        > > news:eU52NSEPEH A.2960@TK2MSFTN GP10.phx.gbl...[color=darkred]
        > > > is there any speed diffrences between doing Ctype or directcast? I[/color][/color][/color]
        know[color=blue][color=green][color=darkred]
        > > > about the inherite diffrences, but process usage time wise, does one[/color][/color]
        > take[color=green]
        > > up[color=darkred]
        > > > more cycles then the other? thanks
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Brian Henry

          #5
          Re: speed diffrencets CType and directcast

          thanks everyone


          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
          news:%23R3n3LHP EHA.2256@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > Howard,
          > I believe I stated that in there someplace ;-)
          >
          > My understanding is that DirectCast will only cast so it is faster, while
          > CType may convert and this extra check will slow it down.
          >
          > Realizing that with Option Strict On, the compiler would/should know if
          > CType needs to cast or convert, so a number of times CType & DirectCast[/color]
          will[color=blue]
          > be the same speed (produce the same IL code).
          >
          > However! I don't think it really matters which is faster! IMHO What really
          > matters is which is correct to use for what you are doing! As you stated,
          > DirectCast cannot be used in all circumstances, basically it cannot be[/color]
          used[color=blue]
          > where you need to Convert a value & not cast it!
          >
          > Hope this helps
          > Jay
          >
          > "Howard Kaikow" <kaikow@standar ds.com> wrote in message
          > news:eejP%23ZFP EHA.640@TK2MSFT NGP12.phx.gbl.. .[color=green]
          > > I read something recently, I forget where, that stated DirectCast is[/color]
          > faster,[color=green]
          > > but cannot be used in all circumstances.
          > >
          > > --
          > > http://www.standards.com/; See Howard Kaikow's web site.
          > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in[/color][/color]
          message[color=blue][color=green]
          > > news:%23ZDTq8EP EHA.736@tk2msft ngp13.phx.gbl.. .[color=darkred]
          > > > Brian,
          > > > When CType is simply casting I would not expect there to be any speed
          > > > difference.
          > > >
          > > > However when CType is converting, then I would expect the process of
          > > > converting from one type to a different type to have more overhead[/color][/color][/color]
          then[color=blue][color=green][color=darkred]
          > > > simply casting. However I question if the difference in speed is worth[/color][/color]
          > the[color=green][color=darkred]
          > > > bother.
          > > >
          > > > However! I find its better to write "correct" programs first, then[/color][/color][/color]
          worry[color=blue][color=green][color=darkred]
          > > > about optimizations after a specific routine has been identified, via
          > > > profiling, to have a performance issue.
          > > >
          > > > As you know, CType is the conversion operator used to convert one type[/color]
          > > into[color=darkred]
          > > > a different type, such as a String to an Integer. While DirectCast is[/color][/color]
          > used[color=green][color=darkred]
          > > > for casting, normally from a base type to a derived type, where the[/color][/color][/color]
          base[color=blue][color=green][color=darkred]
          > > > object is of the derived type. Where Type can be classes, structures[/color][/color][/color]
          or[color=blue][color=green][color=darkred]
          > > > interfaces...
          > > >
          > > > Note with overloading the CType operator available in Whidbey (VS.NET[/color][/color]
          > 2005[color=green][color=darkred]
          > > > due out next year) it will be even more important to use the "correct"
          > > > operator. As a CType that fails in VB.NET 2003 may suddenly work in[/color][/color]
          > VB.NET[color=green][color=darkred]
          > > > 2005...
          > > >
          > > > Of course "correct-ness" is subjective ;-)
          > > >
          > > > Hope this helps
          > > > Jay
          > > >
          > > > "Brian Henry" <brianiupmsdn@n ewsgroups.nospa m> wrote in message
          > > > news:eU52NSEPEH A.2960@TK2MSFTN GP10.phx.gbl...
          > > > > is there any speed diffrences between doing Ctype or directcast? I[/color][/color]
          > know[color=green][color=darkred]
          > > > > about the inherite diffrences, but process usage time wise, does one[/color]
          > > take[color=darkred]
          > > > up
          > > > > more cycles then the other? thanks
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Howard Kaikow

            #6
            Re: speed diffrencets CType and directcast

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:%23R3n3LHP EHA.2256@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > Howard,
            > I believe I stated that in there someplace ;-)[/color]

            I saw the statement in a book or MSDN article, not in any newsgroup.


            Comment

            • Eric Lemmon

              #7
              Re: speed diffrencets CType and directcast

              DirectCast is supposedly faster than CType because it is "more native" to
              the .NET Framework. (You would need to have some pretty heavy processing to
              notice a difference, though.)

              Eric


              "Howard Kaikow" <kaikow@standar ds.com> wrote in message
              news:%23gLZW3OP EHA.3016@tk2msf tngp13.phx.gbl. ..[color=blue]
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              > news:%23R3n3LHP EHA.2256@TK2MSF TNGP10.phx.gbl. ..[color=green]
              > > Howard,
              > > I believe I stated that in there someplace ;-)[/color]
              >
              > I saw the statement in a book or MSDN article, not in any newsgroup.
              >
              >[/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: speed diffrencets CType and directcast

                Howard,
                "there" is referring to my post, I believe I made the same statement you
                made in my post.

                Unfortunately my statement was less succinct then yours ;-)

                Jay

                "Howard Kaikow" <kaikow@standar ds.com> wrote in message
                news:%23gLZW3OP EHA.3016@tk2msf tngp13.phx.gbl. ..[color=blue]
                > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                > news:%23R3n3LHP EHA.2256@TK2MSF TNGP10.phx.gbl. ..[color=green]
                > > Howard,
                > > I believe I stated that in there someplace ;-)[/color]
                >
                > I saw the statement in a book or MSDN article, not in any newsgroup.
                >
                >[/color]


                Comment

                Working...