DirectCast/CType speed VS Direct call speed?

M

Matt

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
 
C

Cor Ligthert [MVP]

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
 
H

Herfried K. Wagner [MVP]

Matt said:
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

Matt

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).addline(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
 
C

Cor Ligthert [MVP]

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
 
M

Matt

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.
 

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