473,947 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which approach outperforms the other ?

Hi...

I have the following two excerpts from a method. It basically reads data
from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _
DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I
find
that at each DirectCast, there is unboxing but in Excerpt B, there isn't
any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with
regards to
casting would be much appreciated as well.

Cheers !
Nov 20 '05 #1
11 1404
This is mearly a guess at best...but I would like to learn this as well...

I would assume the direct cast would be faster (mainly because I know direct
cast is faster) and also this call

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
romObject(objec t)

I would figure that FromObject is a class and created on the heap. I would
also assume there is more code inside that method that takes longer to
execute. Whereas the unbox (I don't know what that means necessarily) seems
pretty direct. I don't know, just assuming.

I think it would be helpful to see the dissassembly from the FromObject
method to be 100% sure.

HTH,
CJ

"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi...

I have the following two excerpts from a method. It basically reads data
from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _
DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I
find
that at each DirectCast, there is unboxing but in Excerpt B, there isn't
any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with
regards to
casting would be much appreciated as well.

Cheers !

Nov 20 '05 #2
Tinman,
In addition to the earlier answers William and I gave, the first is more
correct, although I would use the technique I gave in my first response here
instead of DirectCast.

Using Option Strict On, which requires the first, is more "correct", in that
you are stating that project_id is a GUID, start date is a DateTime... You
are not allowing the compiler make that decision for you...

Also using Option Strict On allows you to catch a number of errors at
compile time, instead of run time.

Hope this helps
Jay

"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi...

I have the following two excerpts from a method. It basically reads data
from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _
DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I
find
that at each DirectCast, there is unboxing but in Excerpt B, there isn't
any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with
regards to
casting would be much appreciated as well.

Cheers !

Nov 20 '05 #3
thanks guys....

I believe the Get*Type* works better than the DirectCast approach. The
resulting
IL appears "leaner"...

In the initial IL with DirectCast, I was afraid that all the unboxing would
cause a performance
concern since it was going to continue reading data in a loop (While .Read)
........

Cheers...
"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi...

I have the following two excerpts from a method. It basically reads data
from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _
DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I
find
that at each DirectCast, there is unboxing but in Excerpt B, there isn't
any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32
[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with
regards to
casting would be much appreciated as well.

Cheers !

Nov 20 '05 #4
Alright, maybe this is a dumb question, but what is unboxing?
"tinman" <dw****@yahoo.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
thanks guys....

I believe the Get*Type* works better than the DirectCast approach. The
resulting
IL appears "leaner"...

In the initial IL with DirectCast, I was afraid that all the unboxing would cause a performance
concern since it was going to continue reading data in a loop (While ..Read) .......

Cheers...
"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi...

I have the following two excerpts from a method. It basically reads data
from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _
DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I find
that at each DirectCast, there is unboxing but in Excerpt B, there isn't
any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with
regards to
casting would be much appreciated as well.

Cheers !


Nov 20 '05 #5
CJ,
When you assign a value type to an Object variable that type is boxed. Which
copies the value of the value type to an object on the heap.

When you then assign this Object variable back to the value type variable,
the object is unboxed.

Dim i As Integer
i = 100
Dim o As Object
o = i ' this causes the 100 to be boxed.

i = o ' this causes the boxed 100 to be unboxed

Of course if you have Option Strict On, you need DirectCast

i = DirectCast(o, Integer)

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:Op******** ********@TK2MSF TNGP09.phx.gbl. ..
Alright, maybe this is a dumb question, but what is unboxing?
"tinman" <dw****@yahoo.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
thanks guys....

I believe the Get*Type* works better than the DirectCast approach. The
resulting
IL appears "leaner"...

In the initial IL with DirectCast, I was afraid that all the unboxing would
cause a performance
concern since it was going to continue reading data in a loop (While

.Read)
.......

Cheers...
"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
Hi...

I have the following two excerpts from a method. It basically reads data from a DataReader and
loads it in a collection.
Excerpt A:
********
With ProjectData
While .Read
Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
DirectCast(.Ite m("start_date") , DateTime), _ DirectCast(.Ite m("end_date") , DateTime), _
DirectCast(.Ite m("priority") , Integer), _
DirectCast(.Ite m("description" ), String), _
DirectCast(.Ite m("project_mana ger"), String) End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("p roject_id"),
.Item("start_da te"),
.Item("end_date "),
.Item("priority "),
.Item("descript ion"),
.Item("project_ manager")
End While
End With

Question:

Does automatic casting outperform explicit casting ? When I run ILDASM, I
find
that at each DirectCast, there is unboxing but in Excerpt B, there
isn't any.

Snippets of the ILDASM for Excerpt A and B below:

ILDASM (Excerpt A)
*************** *
IL_0098: ldloc.2
IL_0099: ldstr "descriptio n"
IL_009e: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_00a3: unbox [mscorlib]System.Int32
IL_00a8: ldobj [mscorlib]System.Int32
ILDASM (Excerpt B)
*************** *
IL_0092: ldloc.2
IL_0093: ldstr "descriptio n"
IL_0098: callvirt instance object
XXX.Projects.XD ataReader::get_ Item(string)
IL_009d: call int32

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
romObject(objec t)

Any pointers to articles that explain what goes on "under the hood" with regards to
casting would be much appreciated as well.

Cheers !



Nov 20 '05 #6
Helps tremendously. Thanks for the info on that one Jay.

I think the best way to see it "visually" would be when your watching a
variable (in the watch window) of a base type (i.e. DataRow) thats actually
inherited (i.e MyDataRow of a dataset)

Where it shows your +- graphic and says its a datarow, then there is a sub
type underneath it that tells the actual class type that it is.

I hope that made sense. But again I appreciate your explanation.

So is unboxing an expensive process? given its only a single line of IL but
was curious if it was or not.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:OL******** ******@TK2MSFTN GP10.phx.gbl...
CJ,
When you assign a value type to an Object variable that type is boxed. Which copies the value of the value type to an object on the heap.

When you then assign this Object variable back to the value type variable,
the object is unboxed.

Dim i As Integer
i = 100
Dim o As Object
o = i ' this causes the 100 to be boxed.

i = o ' this causes the boxed 100 to be unboxed

Of course if you have Option Strict On, you need DirectCast

i = DirectCast(o, Integer)

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:Op******** ********@TK2MSF TNGP09.phx.gbl. ..
Alright, maybe this is a dumb question, but what is unboxing?
"tinman" <dw****@yahoo.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
thanks guys....

I believe the Get*Type* works better than the DirectCast approach. The
resulting
IL appears "leaner"...

In the initial IL with DirectCast, I was afraid that all the unboxing would
cause a performance
concern since it was going to continue reading data in a loop (While

.Read)
.......

Cheers...
"tinman" <dw****@yahoo.c om> wrote in message
news:uh******** ******@TK2MSFTN GP12.phx.gbl...
> Hi...
>
> I have the following two excerpts from a method. It basically reads data > from a DataReader and
> loads it in a collection.
>
>
> Excerpt A:
> ********
> With ProjectData
> While .Read
> Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
> DirectCast(.Ite m("start_date") , DateTime), _
> DirectCast(.Ite m("end_date") , DateTime),
_ > DirectCast(.Ite m("priority") , Integer), _
> DirectCast(.Ite m("description" ), String), _ > DirectCast(.Ite m("project_mana ger"),

String) > End While
> End With
>
>
>
>
> Excerpt B:
> ********
> With ProjectData
> While .Read
> Me.Add(.Item("p roject_id"),
> .Item("start_da te"),
> .Item("end_date "),
> .Item("priority "),
> .Item("descript ion"),
> .Item("project_ manager")
> End While
> End With
>
>
>
> Question:
>
> Does automatic casting outperform explicit casting ? When I run ILDASM,
I
> find
> that at each DirectCast, there is unboxing but in Excerpt B, there

isn't > any.
>
> Snippets of the ILDASM for Excerpt A and B below:
>
> ILDASM (Excerpt A)
> *************** *
> IL_0098: ldloc.2
> IL_0099: ldstr "descriptio n"
> IL_009e: callvirt instance object
> XXX.Projects.XD ataReader::get_ Item(string)
> IL_00a3: unbox [mscorlib]System.Int32
> IL_00a8: ldobj [mscorlib]System.Int32
>
>
> ILDASM (Excerpt B)
> *************** *
> IL_0092: ldloc.2
> IL_0093: ldstr "descriptio n"
> IL_0098: callvirt instance object
> XXX.Projects.XD ataReader::get_ Item(string)
> IL_009d: call int32
>

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
> romObject(objec t)
>
> Any pointers to articles that explain what goes on "under the hood" with > regards to
> casting would be much appreciated as well.
>
> Cheers !
>
>



Nov 20 '05 #7
CJ,
So is unboxing an expensive process? given its only a single line of IL but was curious if it was or not. Its expensive in that the value needs to be copied from the heap to the
variable.

Its also expensive in that the boxing itself created an object on the heap,
this object will later need to be Garbage collected.

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. . Helps tremendously. Thanks for the info on that one Jay.

I think the best way to see it "visually" would be when your watching a
variable (in the watch window) of a base type (i.e. DataRow) thats actually inherited (i.e MyDataRow of a dataset)

Where it shows your +- graphic and says its a datarow, then there is a sub
type underneath it that tells the actual class type that it is.

I hope that made sense. But again I appreciate your explanation.

So is unboxing an expensive process? given its only a single line of IL but was curious if it was or not.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:OL******** ******@TK2MSFTN GP10.phx.gbl...
CJ,
When you assign a value type to an Object variable that type is boxed. Which
copies the value of the value type to an object on the heap.

When you then assign this Object variable back to the value type variable,
the object is unboxed.

Dim i As Integer
i = 100
Dim o As Object
o = i ' this causes the 100 to be boxed.

i = o ' this causes the boxed 100 to be unboxed

Of course if you have Option Strict On, you need DirectCast

i = DirectCast(o, Integer)

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:Op******** ********@TK2MSF TNGP09.phx.gbl. ..
Alright, maybe this is a dumb question, but what is unboxing?
"tinman" <dw****@yahoo.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
> thanks guys....
>
> I believe the Get*Type* works better than the DirectCast approach. The > resulting
> IL appears "leaner"...
>
> In the initial IL with DirectCast, I was afraid that all the unboxing would
> cause a performance
> concern since it was going to continue reading data in a loop (While
.Read)
> .......
>
> Cheers...
>
>
> "tinman" <dw****@yahoo.c om> wrote in message
> news:uh******** ******@TK2MSFTN GP12.phx.gbl...
> > Hi...
> >
> > I have the following two excerpts from a method. It basically reads
data
> > from a DataReader and
> > loads it in a collection.
> >
> >
> > Excerpt A:
> > ********
> > With ProjectData
> > While .Read
> > Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
> > DirectCast(.Ite m("start_date") , DateTime),
_
> > DirectCast(.Ite m("end_date") ,

DateTime), _ > > DirectCast(.Ite m("priority") , Integer),
_ > > DirectCast(.Ite m("description" ), String),
_ > > DirectCast(.Ite m("project_mana ger"),

String)
> > End While
> > End With
> >
> >
> >
> >
> > Excerpt B:
> > ********
> > With ProjectData
> > While .Read
> > Me.Add(.Item("p roject_id"),
> > .Item("start_da te"),
> > .Item("end_date "),
> > .Item("priority "),
> > .Item("descript ion"),
> > .Item("project_ manager")
> > End While
> > End With
> >
> >
> >
> > Question:
> >
> > Does automatic casting outperform explicit casting ? When I run

ILDASM,
I
> > find
> > that at each DirectCast, there is unboxing but in Excerpt B, there

isn't
> > any.
> >
> > Snippets of the ILDASM for Excerpt A and B below:
> >
> > ILDASM (Excerpt A)
> > *************** *
> > IL_0098: ldloc.2
> > IL_0099: ldstr "descriptio n"
> > IL_009e: callvirt instance object
> > XXX.Projects.XD ataReader::get_ Item(string)
> > IL_00a3: unbox [mscorlib]System.Int32
> > IL_00a8: ldobj [mscorlib]System.Int32
> >
> >
> > ILDASM (Excerpt B)
> > *************** *
> > IL_0092: ldloc.2
> > IL_0093: ldstr "descriptio n"
> > IL_0098: callvirt instance object
> > XXX.Projects.XD ataReader::get_ Item(string)
> > IL_009d: call int32
> >
>

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F > > romObject(objec t)
> >
> > Any pointers to articles that explain what goes on "under the

hood" with
> > regards to
> > casting would be much appreciated as well.
> >
> > Cheers !
> >
> >
>
>



Nov 20 '05 #8
Ok, last question I swear.

Alright, so unboxing takes a variable (more than likely on the heap in most
cases) and creates a copy of that on the heap again on the unboxing process.
And then has to be gc'd as well as the original value (if that original
value exists on the heap as well).

Hmmm...

Option Strict. =)

Thanks Jay!

-CJ

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
CJ,
So is unboxing an expensive process? given its only a single line of IL but
was curious if it was or not.

Its expensive in that the value needs to be copied from the heap to the
variable.

Its also expensive in that the boxing itself created an object on the

heap, this object will later need to be Garbage collected.

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Helps tremendously. Thanks for the info on that one Jay.

I think the best way to see it "visually" would be when your watching a
variable (in the watch window) of a base type (i.e. DataRow) thats actually
inherited (i.e MyDataRow of a dataset)

Where it shows your +- graphic and says its a datarow, then there is a sub
type underneath it that tells the actual class type that it is.

I hope that made sense. But again I appreciate your explanation.

So is unboxing an expensive process? given its only a single line of IL

but
was curious if it was or not.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:OL******** ******@TK2MSFTN GP10.phx.gbl...
CJ,
When you assign a value type to an Object variable that type is boxed.

Which
copies the value of the value type to an object on the heap.

When you then assign this Object variable back to the value type

variable, the object is unboxed.

Dim i As Integer
i = 100
Dim o As Object
o = i ' this causes the 100 to be boxed.

i = o ' this causes the boxed 100 to be unboxed

Of course if you have Option Strict On, you need DirectCast

i = DirectCast(o, Integer)

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:Op******** ********@TK2MSF TNGP09.phx.gbl. ..
> Alright, maybe this is a dumb question, but what is unboxing?
>
>
> "tinman" <dw****@yahoo.c om> wrote in message
> news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
> > thanks guys....
> >
> > I believe the Get*Type* works better than the DirectCast approach. The > > resulting
> > IL appears "leaner"...
> >
> > In the initial IL with DirectCast, I was afraid that all the unboxing > would
> > cause a performance
> > concern since it was going to continue reading data in a loop (While > .Read)
> > .......
> >
> > Cheers...
> >
> >
> > "tinman" <dw****@yahoo.c om> wrote in message
> > news:uh******** ******@TK2MSFTN GP12.phx.gbl...
> > > Hi...
> > >
> > > I have the following two excerpts from a method. It basically reads data
> > > from a DataReader and
> > > loads it in a collection.
> > >
> > >
> > > Excerpt A:
> > > ********
> > > With ProjectData
> > > While .Read
> > > Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
> > > DirectCast(.Ite m("start_date") ,

DateTime),
_
> > > DirectCast(.Ite m("end_date") , DateTime),
_
> > > DirectCast(.Ite m("priority") , Integer), _ > > > DirectCast(.Ite m("description" ), String),
_
> > > DirectCast(.Ite m("project_mana ger"),
String)
> > > End While
> > > End With
> > >
> > >
> > >
> > >
> > > Excerpt B:
> > > ********
> > > With ProjectData
> > > While .Read
> > > Me.Add(.Item("p roject_id"),
> > > .Item("start_da te"),
> > > .Item("end_date "),
> > > .Item("priority "),
> > > .Item("descript ion"),
> > > .Item("project_ manager")
> > > End While
> > > End With
> > >
> > >
> > >
> > > Question:
> > >
> > > Does automatic casting outperform explicit casting ? When I run
ILDASM,
> I
> > > find
> > > that at each DirectCast, there is unboxing but in Excerpt B,

there isn't
> > > any.
> > >
> > > Snippets of the ILDASM for Excerpt A and B below:
> > >
> > > ILDASM (Excerpt A)
> > > *************** *
> > > IL_0098: ldloc.2
> > > IL_0099: ldstr "descriptio n"
> > > IL_009e: callvirt instance object
> > > XXX.Projects.XD ataReader::get_ Item(string)
> > > IL_00a3: unbox [mscorlib]System.Int32
> > > IL_00a8: ldobj [mscorlib]System.Int32
> > >
> > >
> > > ILDASM (Excerpt B)
> > > *************** *
> > > IL_0092: ldloc.2
> > > IL_0093: ldstr "descriptio n"
> > > IL_0098: callvirt instance object
> > > XXX.Projects.XD ataReader::get_ Item(string)
> > > IL_009d: call int32
> > >
> >
>

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
> > > romObject(objec t)
> > >
> > > Any pointers to articles that explain what goes on "under the hood" with
> > > regards to
> > > casting would be much appreciated as well.
> > >
> > > Cheers !
> > >
> > >
> >
> >
>
>



Nov 20 '05 #9
CJ,
Alright, so unboxing takes a variable (more than likely on the heap in most cases) By definition a boxed value is on the heap.
cases) and creates a copy of that on the heap again on the unboxing process.
No, not quite. Unboxing creates a copy of that value on the stack.
And then has to be gc'd as well as the original value (if that original
value exists on the heap as well). Correct the object that was created when the original value was boxed will
need to GC'd...

If you following the above, remember that a regular reference type (a Class)
can contain value types also, these values can also be boxed & unboxed,
however explaining it is a little more confusing ;-)

Option Strict has no real effect on Boxing & Unboxing.

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:uv******** ********@TK2MSF TNGP11.phx.gbl. .. Ok, last question I swear.

Alright, so unboxing takes a variable (more than likely on the heap in most cases) and creates a copy of that on the heap again on the unboxing process. And then has to be gc'd as well as the original value (if that original
value exists on the heap as well).

Hmmm...

Option Strict. =)

Thanks Jay!

-CJ

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
CJ,
So is unboxing an expensive process? given its only a single line of IL
but
was curious if it was or not.

Its expensive in that the value needs to be copied from the heap to the
variable.

Its also expensive in that the boxing itself created an object on the

heap,
this object will later need to be Garbage collected.

Hope this helps
Jay

"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Helps tremendously. Thanks for the info on that one Jay.

I think the best way to see it "visually" would be when your watching
a variable (in the watch window) of a base type (i.e. DataRow) thats

actually
inherited (i.e MyDataRow of a dataset)

Where it shows your +- graphic and says its a datarow, then there is a sub type underneath it that tells the actual class type that it is.

I hope that made sense. But again I appreciate your explanation.

So is unboxing an expensive process? given its only a single line of IL but
was curious if it was or not.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:OL******** ******@TK2MSFTN GP10.phx.gbl...
> CJ,
> When you assign a value type to an Object variable that type is
boxed. Which
> copies the value of the value type to an object on the heap.
>
> When you then assign this Object variable back to the value type

variable,
> the object is unboxed.
>
> Dim i As Integer
> i = 100
> Dim o As Object
> o = i ' this causes the 100 to be boxed.
>
> i = o ' this causes the boxed 100 to be unboxed
>
> Of course if you have Option Strict On, you need DirectCast
>
> i = DirectCast(o, Integer)
>
> Hope this helps
> Jay
>
> "CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
> news:Op******** ********@TK2MSF TNGP09.phx.gbl. ..
> > Alright, maybe this is a dumb question, but what is unboxing?
> >
> >
> > "tinman" <dw****@yahoo.c om> wrote in message
> > news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
> > > thanks guys....
> > >
> > > I believe the Get*Type* works better than the DirectCast approach.
The
> > > resulting
> > > IL appears "leaner"...
> > >
> > > In the initial IL with DirectCast, I was afraid that all the

unboxing
> > would
> > > cause a performance
> > > concern since it was going to continue reading data in a loop

(While > > .Read)
> > > .......
> > >
> > > Cheers...
> > >
> > >
> > > "tinman" <dw****@yahoo.c om> wrote in message
> > > news:uh******** ******@TK2MSFTN GP12.phx.gbl...
> > > > Hi...
> > > >
> > > > I have the following two excerpts from a method. It basically

reads
> data
> > > > from a DataReader and
> > > > loads it in a collection.
> > > >
> > > >
> > > > Excerpt A:
> > > > ********
> > > > With ProjectData
> > > > While .Read
> > > > Me.Add(DirectCa st(.Item("proje ct_id"), Guid), _
> > > > DirectCast(.Ite m("start_date") ,
DateTime),
> _
> > > > DirectCast(.Ite m("end_date") ,

DateTime),
_
> > > > DirectCast(.Ite m("priority") , Integer),
_
> > > > DirectCast(.Ite m("description" ),

String),
_
> > > >

DirectCast(.Ite m("project_mana ger"), > String)
> > > > End While
> > > > End With
> > > >
> > > >
> > > >
> > > >
> > > > Excerpt B:
> > > > ********
> > > > With ProjectData
> > > > While .Read
> > > > Me.Add(.Item("p roject_id"),
> > > > .Item("start_da te"),
> > > > .Item("end_date "),
> > > > .Item("priority "),
> > > > .Item("descript ion"),
> > > > .Item("project_ manager")
> > > > End While
> > > > End With
> > > >
> > > >
> > > >
> > > > Question:
> > > >
> > > > Does automatic casting outperform explicit casting ? When I run > ILDASM,
> > I
> > > > find
> > > > that at each DirectCast, there is unboxing but in Excerpt B,

there > isn't
> > > > any.
> > > >
> > > > Snippets of the ILDASM for Excerpt A and B below:
> > > >
> > > > ILDASM (Excerpt A)
> > > > *************** *
> > > > IL_0098: ldloc.2
> > > > IL_0099: ldstr "descriptio n"
> > > > IL_009e: callvirt instance object
> > > > XXX.Projects.XD ataReader::get_ Item(string)
> > > > IL_00a3: unbox [mscorlib]System.Int32
> > > > IL_00a8: ldobj [mscorlib]System.Int32
> > > >
> > > >
> > > > ILDASM (Excerpt B)
> > > > *************** *
> > > > IL_0092: ldloc.2
> > > > IL_0093: ldstr "descriptio n"
> > > > IL_0098: callvirt instance object
> > > > XXX.Projects.XD ataReader::get_ Item(string)
> > > > IL_009d: call int32
> > > >
> > >
> >
>

[Microsoft.Visua lBasic]Microsoft.Visua lBasic.Compiler Services.Intege rType::F
> > > > romObject(objec t)
> > > >
> > > > Any pointers to articles that explain what goes on "under the

hood"
> with
> > > > regards to
> > > > casting would be much appreciated as well.
> > > >
> > > > Cheers !
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #10

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

Similar topics

3
305
by: william | last post by:
Hi, everyone After adding a control into a container like form using form.controls.add(myControl), how could I reference it For example dim aControl as new butto me.controls.add(aControl then I want set backcolor of this button, how Thanks
24
1772
by: Brad Marts | last post by:
I would like to have a function that takes as an argument a base class but performs differently depending on which type of derived class is passed. Can I tell which derived class is passed? For example: class Base { //... };
17
2596
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this with a link to old information that suggests use of "navigator": http://developer.irt.org/script/43.htm
14
2386
by: J.S. | last post by:
In a Windows Form application, which is the better method to concatenate large blocks of code? 1. Reading the text from text files. 2. Adding the text to the VB file itself? Thanks! J.S. --
34
10884
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have VBA code that wrote the results of that away to the db, either creating new records or updating existing records, whichever was relevant. This may also include deleting records. Now I generally do this by opening a recordset on the source data...
15
3214
by: Rob Meade | last post by:
Hi all, I have a databse which I'm pulling the data from for my ASP page. I have 4 tables, Course, Feature, Objective, and PreRequisite. The last three all contain a course product code and a text column - the course product code links the row(s) to the Course table. So, I might have 1 entry in Course, but perhaps 3 in Feature, 5 in Objective and 2 in PreRequisite.
2
2238
by: rn5a | last post by:
A Form has a select list which is populated from a MS-Access database table. The DB table from where the select list is populated has 2 columns - CountryID & CountryName. When the Form is posted, the option selected in the select list is inserted in another DB table wherein the column in which the value of the selected option will be populated accepts only integers. Actually there are 7-8 select lists & the values of all the selected...
20
3144
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
0
10163
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
11576
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
11160
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...
0
9888
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...
0
6116
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4946
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
2
4538
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3543
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.