468,783 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

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
Nov 20 '05 #1
7 2365
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" <br**********@newsgroups.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2
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]" <Ja************@msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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" <br**********@newsgroups.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3
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" <ka****@standards.com> wrote in message
news:ee***************@TK2MSFTNGP12.phx.gbl...
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]" <Ja************@msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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" <br**********@newsgroups.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #4
thanks everyone
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <ka****@standards.com> wrote in message
news:ee***************@TK2MSFTNGP12.phx.gbl...
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]" <Ja************@msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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" <br**********@newsgroups.nospam> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #5
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Howard,
I believe I stated that in there someplace ;-)


I saw the statement in a book or MSDN article, not in any newsgroup.
Nov 20 '05 #6
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" <ka****@standards.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Howard,
I believe I stated that in there someplace ;-)


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

Nov 20 '05 #7
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" <ka****@standards.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Howard,
I believe I stated that in there someplace ;-)


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

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joeri | last post: by
13 posts views Thread by Carl Howarth | last post: by
6 posts views Thread by Ot | last post: by
8 posts views Thread by John A Grandy | last post: by
1 post views Thread by iwdu15 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.