473,835 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2720
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**********@n ewsgroups.nospa m> wrote in message
news:eU******** ******@TK2MSFTN GP10.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******** *******@tk2msft ngp13.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**********@n ewsgroups.nospa m> wrote in message
news:eU******** ******@TK2MSFTN GP10.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****@standar ds.com> wrote in message
news:ee******** *******@TK2MSFT NGP12.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******** *******@tk2msft ngp13.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**********@n ewsgroups.nospa m> wrote in message
news:eU******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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****@standar ds.com> wrote in message
news:ee******** *******@TK2MSFT NGP12.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******** *******@tk2msft ngp13.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**********@n ewsgroups.nospa m> wrote in message
news:eU******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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****@standar ds.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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****@standar ds.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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
6578
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 inherits from clsA
13
1680
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 datareader is selected. The function accepts a 'sender' object, which may be of a different type depending on how the function is ran. In one instance the type is "System.Web.UI.WebControls.RepeaterItem", and on others it is...
4
3843
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 absolutely no added functionality to it.
6
10834
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 found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
8
7359
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
5068
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
4110
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 convert it -- -iwdu15
5
2616
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 j.add("string") directcast(j,collections.arraylist).add("string2")
7
1311
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 realy expensive (time wise) : '------------------------------------------------------------------ 1 For n As Int32 = 1 To Me._transColl.Count - 1 2 ti = CType(Me._transColl(n), TranslateImport)
0
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10517
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10559
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9343
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7765
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6961
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.