473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

DirectCast/CType speed VS Direct call speed?

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")
whats the speed difference between j.add or the directcast call?

Cheers,
Matt
Nov 21 '05 #1
5 2587
Matt,

The second one (with casting) will cost more time (however probably it is
optimized by the JIT) because it is completly needless.

Casting you use for using the type from a derived class while using the
parent class type or an interface as the handled object.

I hope this helps,

Cor
Nov 21 '05 #2
"Matt" <ma*****@interactiveideasandsolutions.com> schrieb:
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")
whats the speed difference between j.add or the directcast call?


I suggest to take a look at the IL emitted by the compiler using the ILDASM
tool. Note that casting in the second case doesn't make sense because 'j''s
type is already 'Collections.ArrayList'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Yeah i understand that it makes no sense to do that. i didnt really
explain myself properly i dont think. Im not actually intending to use
it instead of the add method. hehe that would be kinda silly.

// Take this code for example.

dim j as collections.arraylist()
dim dgraphics as drawing.graphics

for i= 0 to 100
dim objPath = new drawing2d.graphicspath()
j.add(objPath)
objpath = nothing
next

// Now to access it
// i cant use it like this because it gives a compile error

for index = 0 to 100
j(index).addline(x1,y1,x2,y2)
next

// so it has to be access like this

for index = 0 to 100
directcast(j(index),drawing2d.graphicspath).addlin e(x1,y1,x2,y2)
next

// End of code

So either i use DirectCast or CType OR i create my own collection class
specifically to deal with this problem since i know that only one type
of object is going to be in there.

So my real question is, is the speed difference worth worrying about
between a direct call (like .add), or casting through directcast or ctype?

Keep in mind that this program is going to end up being fairly CPU
intensive in its drawing. And if you dont know the answer, thats ok. i
dont think anyone has actually done any research on it yet coz google
couldnt help me.

Thanks Heaps,

Matt
Cor Ligthert [MVP] wrote:
Matt,

The second one (with casting) will cost more time (however probably it is
optimized by the JIT) because it is completly needless.

Casting you use for using the type from a derived class while using the
parent class type or an interface as the handled object.

I hope this helps,

Cor

Nov 21 '05 #4
Matt,

Casting (DirectCast) cost time as every thing cost time. Converting (by
instance CType) cost even more time.

If you have to do this often in your program you can better create your own
class (stronly typed class).

If you cast/convert only some times you will loose the advantage of creating
that class by the code that is involved in that.

I hope that this short explanation gives an idea?

Cor
Nov 21 '05 #5
Yup that gies me a good idea of my direction. I probably need to just
keep an eye on my programs speed so far and if it gets too much then ill
move to a user defined class instead of the standard arraylist.

It looks like i may have to anyway, considering some of the problems i
have run into today.

Thanks Cor.
Cor Ligthert [MVP] wrote:
Matt,

Casting (DirectCast) cost time as every thing cost time. Converting (by
instance CType) cost even more time.

If you have to do this often in your program you can better create your own
class (stronly typed class).

If you cast/convert only some times you will loose the advantage of creating
that class by the code that is involved in that.

I hope that this short explanation gives an idea?

Cor

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
13
by: Crirus | last post by:
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object. The run-time tipe is a derived of a class, but I need to refer to that instance...
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...
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()
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
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
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...
4
by: Vagabond Software | last post by:
Here is the code: Dim amountDue As Double amountDue = Math.Round(DirectCast(strOrderTotal, Double), 2) This code produces an invalid cast exception. The String variable strOrderTotal...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
If Visual Studio knows the type, why does the system-generated property use CType instead of DirectCast? DirectCast is more efficient right? For example - Here's what we have for a setting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.