472,791 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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 2639
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD...
13
by: Carl Howarth | last post by:
Hello there, I am currently in the process of 'getting strict' with some of our aspx.vb files. I have a function that is called either when a datarepeater is bound or when a button on the...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
8
by: John A Grandy | last post by:
could someone please discuss the pros and cons of CType(MyDouble,Decimal) versus Convert.ToDecimal(MyDouble) .... and other such conversions ...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
1
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can...
5
by: Matt | last post by:
Does anyone know or care to find out the speed difference between a directcast call and a direct call? for instance, i might have these lines of code: dim j as collections.arraylist ...
7
by: Peter | last post by:
I have the following code: _transColl is a collection of classes and it contains about 42000 objects My question is there any way I can make this code run faster? I have found out that line 2 is...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.