哪种方法优于另一种方法? [英] which approach outperforms the other ?

查看:92
本文介绍了哪种方法优于另一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...


我有一个方法的以下两个摘录。它基本上从DataReader读取数据

并且

将其加载到集合中。

摘录A:

* *******

使用ProjectData

而.Read

Me.Add(DirectCast(.Item(" project_id")) ,Guid),_

DirectCast(.Item(" start_date"),DateTime),_

DirectCast(.Item(" end_date"),DateTime), _

DirectCast(.Item(" priority"),Integer),_

DirectCast(.Item(" description"),String),_

DirectCast(.Item(" project_manager"),String)

结束时

结束



摘录B:

********

使用ProjectData

而.Read

Me.Add(.Item(" project_id"),

.Item(" start_date"),

.Item(" end_date"),

.Item(" priority"),

.Item(" description"),

.Item(" project_manager")

E. nd While

结束


问题:


自动投射是否优于显式投射?当我运行ILDASM时,我发现

在每个DirectCast上都有拆箱但是在摘录B中,没有任何
任何。


以下摘录A和B的ILDASM片段:


ILDASM(摘录A)

* ***************

IL_0098:ldloc.2

IL_0099:ldstr" description"

IL_009e:callvirt实例对象

XXX.Projects.XDataReader :: get_Item(string)

IL_00a3:unbox [mscorlib] System.Int32

IL_00a8:ldobj [mscorlib] System.Int32

ILDASM(摘录B)

****************

IL_0092:ldloc.2

IL_0093:ldstr" description"

IL_0098:callvirt实例对象

XXX.Projects.XDataReader :: get_Item(string)

IL_009d:call int32

[Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices.IntegerType :: F

romObject (对象)


任何指针文章解释了引擎盖下发生的事情。与

关于

铸造也将非常受欢迎。


干杯!

解决方案

这是最好的猜测...但我也想学习这个...


我会假设直接投射会更快(主要是因为我知道直接

施法更快)而且这个调用


[Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices。 IntegerType :: F

romObject(object)


我认为FromObject是一个类并在堆上创建。我会

也假设该方法中有更多的代码需要更长的时间来执行
。虽然unbox(我不知道这意味着什么)似乎很可靠。

非常直接。我不知道,只是假设。


我认为从FromObject

方法看到反汇编是100%肯定会有所帮助。


HTH,

CJ


" tinman" < DW **** @ yahoo.com>在消息中写道

新闻:呃************** @ TK2MSFTNGP12.phx.gbl ...

嗨...

我有一个方法的以下两个摘录。它基本上从DataReader读取数据并将其加载到集合中。

摘录A:
********
随着ProjectData
当.Read
Me.Add(DirectCast(.Item(" project_id"),Guid),_
DirectCast(.Item(" start_date"),DateTime),_
DirectCast(.Item(" end_date"),DateTime),_
DirectCast(.Item(" priority"),Integer),_
DirectCast(.Item(" description") ;),String),_
DirectCast(.Item(" project_manager"),String)
结束时


摘录B :
********
使用ProjectData
而.Read
Me.Add(.Item(" project_id"),
。Item( start_date),
。Item(" end_date"),
.Item(" priority"),
。Item(" description"),
。Item(" project_manager")
结束时
结束

问题:

自动投射是否优于显式投射?当我运行ILDASM时,我发现
在每个DirectCast上都有拆箱但是在摘录B中,没有任何内容。

以下摘录A和B的ILDASM:

ILDASM(摘录A)
****************
IL_0098:ldloc.2
IL_0099:ldstr" description"
IL_009e:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_00a3:unbox [mscorlib] System.Int32
IL_00a8:ldobj [mscorlib] System.Int32

ILDASM(摘录B)
****************
IL_0092: ldloc.2
IL_0093:ldstr" description"
IL_0098:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_009d:调用int32

[Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices.IntegerType :: F romObject(object)

指向文章的任何指针都可以解释引擎盖下的内容。
关于
铸造也将非常感激。

干杯!



< blockquote> Tinman,

除了William和我给出的早期答案之外,第一个是更正确的,但是我会在第一次回复时使用我给出的技术

而不是DirectCast。


使用Option Strict On,需要第一个,更正确,其中

你说的是project_id是一个GUID,开始日期是一个DateTime ...你

不允许编译器为你做出决定...


另外使用Option Strict On允许你在编译时间而不是运行时捕获一些错误。


希望这有助于

Jay


" tinman" < DW **** @ yahoo.com>在消息中写道

新闻:呃************** @ TK2MSFTNGP12.phx.gbl ...

嗨...

我有一个方法的以下两个摘录。它基本上从DataReader读取数据并将其加载到集合中。

摘录A:
********
随着ProjectData
当.Read
Me.Add(DirectCast(.Item(" project_id"),Guid),_
DirectCast(.Item(" start_date"),DateTime),_
DirectCast(.Item(" end_date"),DateTime),_
DirectCast(.Item(" priority"),Integer),_
DirectCast(.Item(" description") ;),String),_
DirectCast(.Item(" project_manager"),String)
结束时


摘录B :
********
使用ProjectData
而.Read
Me.Add(.Item(" project_id"),
。Item( start_date),
。Item(" end_date"),
.Item(" priority"),
。Item(" description"),
。Item(" project_manager")
结束时
结束

问题:

自动投射是否优于显式投射?当我运行ILDASM时,我发现
在每个DirectCast上都有拆箱但是在摘录B中,没有任何内容。

以下摘录A和B的ILDASM:

ILDASM(摘录A)
****************
IL_0098:ldloc.2
IL_0099:ldstr" description"
IL_009e:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_00a3:unbox [mscorlib] System.Int32
IL_00a8:ldobj [mscorlib] System.Int32

ILDASM(摘录B)
****************
IL_0092: ldloc.2
IL_0093:ldstr" description"
IL_0098:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_009d:调用int32

[Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices.IntegerType :: F romObject(object)

指向文章的任何指针都可以解释引擎盖下的内容。
关于
铸造也将非常感激。

干杯!



< blockquote>谢谢你们....


我相信Get * Type *比DirectCast方法效果更好。结果



IL显得更精简...


在使用DirectCast的最初IL中,我担心所有拆箱都会导致性能问题因为它会继续读取循环中的数据(而。读取)

.. ......


干杯......

" tinman" < DW **** @ yahoo.com>在消息中写道

新闻:呃************** @ TK2MSFTNGP12.phx.gbl ...

嗨...

我有一个方法的以下两个摘录。它基本上从DataReader读取数据并将其加载到集合中。

摘录A:
********
随着ProjectData
当.Read
Me.Add(DirectCast(.Item(" project_id"),Guid),_
DirectCast(.Item(" start_date"),DateTime),_
DirectCast(.Item(" end_date"),DateTime),_
DirectCast(.Item(" priority"),Integer),_
DirectCast(.Item(" description") ;),String),_
DirectCast(.Item(" project_manager"),String)
结束时


摘录B :
********
使用ProjectData
而.Read
Me.Add(.Item(" project_id"),
。Item( start_date),
。Item(" end_date"),
.Item(" priority"),
。Item(" description"),
。Item(" project_manager")
结束时
结束

问题:

自动投射是否优于显式投射?当我运行ILDASM时,我发现
在每个DirectCast上都有拆箱但是在摘录B中,没有任何内容。

以下摘录A和B的ILDASM:

ILDASM(摘录A)
****************
IL_0098:ldloc.2
IL_0099:ldstr" description"
IL_009e:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_00a3:unbox [mscorlib] System.Int32
IL_00a8:ldobj [mscorlib] System.Int32

ILDASM(摘录B)
****************
IL_0092: ldloc.2
IL_0093:ldstr" description"
IL_0098:callvirt实例对象
XXX.Projects.XDataReader :: get_Item(string)
IL_009d:调用int32

[Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices.IntegerType :: F romObject(object)

指向文章的任何指针都可以解释引擎盖下的内容。
关于
铸造也将非常感激。

干杯!



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(DirectCast(.Item("project_id"), Guid), _
DirectCast(.Item("start_date"), DateTime), _
DirectCast(.Item("end_date"), DateTime), _
DirectCast(.Item("priority"), Integer), _
DirectCast(.Item("description"), String), _
DirectCast(.Item("project_manager"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("project_id"),
.Item("start_date"),
.Item("end_date"),
.Item("priority"),
.Item("description"),
.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 "description"
IL_009e: callvirt instance object
XXX.Projects.XDataReader::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 "description"
IL_0098: callvirt instance object
XXX.Projects.XDataReader::get_Item(string)
IL_009d: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType ::F
romObject(object)

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

Cheers !

解决方案

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.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType ::F
romObject(object)

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.com> wrote in message
news:uh**************@TK2MSFTNGP12.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(DirectCast(.Item("project_id"), Guid), _
DirectCast(.Item("start_date"), DateTime), _
DirectCast(.Item("end_date"), DateTime), _
DirectCast(.Item("priority"), Integer), _
DirectCast(.Item("description"), String), _
DirectCast(.Item("project_manager"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("project_id"),
.Item("start_date"),
.Item("end_date"),
.Item("priority"),
.Item("description"),
.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 "description"
IL_009e: callvirt instance object
XXX.Projects.XDataReader::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 "description"
IL_0098: callvirt instance object
XXX.Projects.XDataReader::get_Item(string)
IL_009d: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType ::F romObject(object)

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

Cheers !



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.com> wrote in message
news:uh**************@TK2MSFTNGP12.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(DirectCast(.Item("project_id"), Guid), _
DirectCast(.Item("start_date"), DateTime), _
DirectCast(.Item("end_date"), DateTime), _
DirectCast(.Item("priority"), Integer), _
DirectCast(.Item("description"), String), _
DirectCast(.Item("project_manager"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("project_id"),
.Item("start_date"),
.Item("end_date"),
.Item("priority"),
.Item("description"),
.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 "description"
IL_009e: callvirt instance object
XXX.Projects.XDataReader::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 "description"
IL_0098: callvirt instance object
XXX.Projects.XDataReader::get_Item(string)
IL_009d: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType ::F romObject(object)

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

Cheers !



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.com> wrote in message
news:uh**************@TK2MSFTNGP12.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(DirectCast(.Item("project_id"), Guid), _
DirectCast(.Item("start_date"), DateTime), _
DirectCast(.Item("end_date"), DateTime), _
DirectCast(.Item("priority"), Integer), _
DirectCast(.Item("description"), String), _
DirectCast(.Item("project_manager"), String)
End While
End With


Excerpt B:
********
With ProjectData
While .Read
Me.Add(.Item("project_id"),
.Item("start_date"),
.Item("end_date"),
.Item("priority"),
.Item("description"),
.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 "description"
IL_009e: callvirt instance object
XXX.Projects.XDataReader::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 "description"
IL_0098: callvirt instance object
XXX.Projects.XDataReader::get_Item(string)
IL_009d: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType ::F romObject(object)

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

Cheers !



这篇关于哪种方法优于另一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆