speed diffrencets CType and directcast

B

Brian Henry

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
H

Howard Kaikow

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

Jay B. Harlow [MVP - Outlook]

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
 
B

Brian Henry

thanks everyone


Jay B. Harlow said:
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
 
E

Eric Lemmon

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top