且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相对成本与。相比

更新时间:2023-02-27 14:47:39

" Rob S" < ro ********* @ hotmail.comwrote:
"Rob S" <ro*********@hotmail.comwrote:

> SuperWidgetWidgets的数量可能从4到45不等,所以这个事件会被触发从少于10次到近50次,具体取决于SuperWidget的特定实例。 (最终可能会有数十个SuperWidgets在一天内创建并销毁。)
>The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)



所以我们在谈论发生的事情的成本最多约*

每天5000次*?

So we''re talking about the cost of something that happens up to about
5,000 times a *day*?


>功能上,两者之间有什么区别,而且是
>Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?



不,不是每天5000次,即使只是在一小部分b $ b $毫秒内。

PS。很抱歉给出一个讽刺的答案。我不知道你问题的真实答案

。我只是觉得有一个巨大的集体

不能看见木头换树在microsoft.csharp新闻组中,当它出现在效率问题上时,我会抓住!


-

Lucian

No, not for a 5000-times-a-day even that''s over in a fraction of a
millisecond.
PS. I''m sorry to give a facetious answer. I don''t know the real answer
to your question. I just think there''s a giant collective
"can''t-see-wood-for-trees" in the microsoft.csharp newsgroups when it
comes to questions of efficiency, and I snap!

--
Lucian


嗨Rob,

两种方法之间的区别几乎没有。我刚刚做了一些

测试和使用as的区别。 vs是超过1,000,000次迭代是1ms

- 所以没有什么可以失去任何睡眠,特别是你打电话的那个数字很低,你更有可能达到性能

在你的一个算法中的其他地方发出问题。我刚刚在旧的

博客上发布了一些内容,请参阅: http: //markrdawson.blogspot.com/

响应使用字符串来比较看起来太开放的类名

以获得微妙的错误插入你的代码,***远离。


理想情况下,在我看来使用as和is,是一种丑陋的OOP解决方案,

a控制器类型对象不应该担心具体的具体具体类型,每个类应该知道如何处理自己。你应该把b / b
抽象出一个接口或抽象类的功能,然后让你知道你的SuperWidget只是调用一些抽象而不是担心

具体具体课程。


马克。

-
http://www.markdawson.org

" Rob S"写道:
Hi Rob,
the difference between the two methods is virtually nothing. I did some
testing on this a while ago and the difference of using "as" vs "is" was 1ms
over 1,000,000 iterations - so nothing to lose any sleep over, especially the
low number you are calling, it is much more likely you will hit a performance
issue somewhere else in one of your algorithms. I posted something to an old
blog a while ago, see: http://markrdawson.blogspot.com/

In response to using strings to compare class names that just seems too open
for subtle bugs to slip into your code, best to stay away.

Also ideally in my opinion using as and is, is a kind of ugly OOP solution,
a controller type object should not need to worry about the specific concrete
type of a class, each class should know how to handle itself. You should
abstract out the functionality into an interface or abstract class and let
you SuperWidget just call into some abstraction rather than worrying about
specific concrete classes.

Mark.
--
http://www.markdawson.org
"Rob S" wrote:

我是C#的新手,一个项目的新手,并且想知道两种处理方式之间的差异

我们的代码中的特殊情况。在

简言之,是之间的性能差异是什么?和as

操作?


基本上,每次子对象触发时都会触发一个事件

添加到父对象 - 称之为SuperWidget。就其本质而言,添加了一些类型的子对象的单个

实例(SuperWidgetHeader,

SuperWidgetFooter等),以及另一种类型的多个实例
$添加了b $ b对象(SuperWidgetWidget)。 SuperWidgetWidgets的数量

可能从4到45不等,所以这个事件会从少于
10到几乎50次触发,具体取决于

SuperWidget。 (最终可能有几十个,所以SuperWidgets创建了

并且全天销毁。)


我们需要对SuperWidgetHeader进行一些特殊处理,所以在

事件处理程序我们正在使用类似的东西:

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;

if(null! = playObject)然后

{

playObject.SomeMethod()

...
I''m new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?

Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)

We need to do some special handling for the SuperWidgetHeader, so in
the event handler we''re using something like:

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...

从可读性的角度来看,我认为这是更可取的:
From a readability standpoint, I think this is preferable:



if(sourceObject is SuperWidgetHeader)

{

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;

playObject.SomeMethod();

...


但是我'我告诉第一个版本效率更高,因为as>
铸件比is更便宜。铸造(更不用说在

第二版中,一旦我们找到合适的物体类型,我们必须再次施放
)。他们不能指我参考这个,

我也无法挖掘任何东西。


功能上,这两者之间的区别是什么?为了便于阅读,为了价值而牺牲了吗?


为了它的价值,研究表明效率更高处理方式

这可能就像:


if(sourceObject.ToString()==" SuperWidgetHeader")

$

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;

playObject.SomeMethod();

...
>
因为(取决于SuperWidget如何实现它)ToString()可能

完全避免昂贵的投射问题,但我们似乎都同意

嵌入字符串用于比较目的的代码与整个对象哲学背道而驰,因此我们完全拒绝这种

方法。这种想法有多么无效?


无论如何,回到最初的问题 - 与...之间的差异是什么?和as?


if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

but I''m told the first version is more efficient because the "as"
casting is less expensive than the "is" casting (not to mention in the
second version once we find the right type of object, we have to cast
it a second time). They can''t point me to a reference to this though,
and I haven''t been able to dig up anything, either.

Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?

For what its worth, research suggests a more efficient way to handle
this may just be something like:

if (sourceObject.ToString() == "SuperWidgetHeader")
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

because (depending upon how SuperWidget impliments it) ToString() may
avoid the expensive casting issues altogether, but we all seem to agree
embedding strings in the code for comparison purposes runs counter to
the whole object philosophy, so we''re pretty much rejecting this
approach completely. How invalid is this thinking?

At any rate, back to the original question - what''s the difference
between "is" and "as"?


当我在搜索最近的帖子时,其他人发布了很好的

答案;最近的讨论(描述ILcastclass等)在这里是



http://groups.google.co.uk/group/mic...0afbfd625b7872


Marc

While I was searching for the recent thread others have posted good
answers; the recent discussion (describing the IL "castclass" etc) is
here:

http://groups.google.co.uk/group/mic...0afbfd625b7872

Marc