这种语法的任何优点...... [英] Any advantage to this syntax...

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

问题描述

这是:


A = B = C;


效率更高:


A = C;

B = C;


在编译的应用程序中?


谢谢。


J

Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J

推荐答案

< rl ***** @newsgroups.nospamwrote:
<rl*****@newsgroups.nospamwrote:

这是:


A = B = C;
Is this:

A = B = C;



这当然更加模糊。任何性能上的差异都会几乎无法衡量,并且肯定不值得担心,因为

由于优化改进而可能会发生变化。

future。底部的代码可以被翻译成上面的代码,

,反之亦然,由编译器自动转换,具体取决于哪个
表现更好。最终,他们应该执行相同的操作。


我个人认为在表达式中使用赋值是不好的风格

,除非在某些非常有限的情况下。对于某些类型的初始化,存在边界

的理由,例如:


counterA = counterB = 0;


当然,从TextReader中读取项目是惯用的:


while((line = r.ReadLine())!= null){...}


在这些事情之外,我不认为这是非常好的想法。

It''s certainly more obscure. Any difference in performance would be
almost unmeasurable though, and certainly not worth worrying about, as
it would be liable to change due to optimization improvements in the
future. The code at the bottom could be translated into the code above,
or vice versa, automatically by the compiler depending on which one
performs better. Ultimately, they ought to perform the same.

I personally consider it bad style to use assignment in an expression
except in certain, very limited scenarios. There is a borderline
justification for certain kinds of initialization, such as in:

counterA = counterB = 0;

And of course, reading items from a TextReader is idiomatic:

while ((line = r.ReadLine()) != null) { ... }

Outside of these kinds of things, I don''t think it''s a terribly good
idea.


效率更高:


A = C;

B = C;


In编译的应用程序?
Any more efficient than:

A = C;
B = C;

In the compiled application?



稍微接近翻译(C#3.0)将是:


var temp = C;

B = temp;

A = temp;


如果例如,您的翻译会有所不同C是一个复杂的

表达式或属性,并且B在C之前被赋值,如果A是属性并且碰巧对B有一些奇怪的依赖,那么可能是


in setter。


- Barry


-
http://barrkel.blogspot.com/


rl*****@newsgroups.nosp 上午写道:
rl*****@newsgroups.nospam wrote:

这是:


A = B = C;


效率更高:


A = C;

B = C;


在编译的应用程序中?


谢谢。


J
Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J



最有可能将它们优化为相同的代码。


即使代码不同,也很难预测哪些代码会实际上表现更好。如果它被编译成两个独立的加载存储器&b
操作,那么这实际上可能比加载存储商店

序列更好,因为这两个操作可能是由

处理器并行执行。


另外,如果存在差异,那么它非常小,你只会

能够测量它,如果你重复操作很多次。


(这当然假设C不是属性或巨大的

结构,但是一个常规的简单变量,比如一个本地的int。)


-

G?ran Andersson

_____
http://www.guffa.com


感谢您的回复。我正在使用它来初始化一堆属性,或者在某些情况下,清除复位函数中的一堆预处理。我喜欢更紧凑的语法,我开始想知道它是否更高效,或者更方便。


J

< rl *** **@newsgroups.nospamwrote在留言新闻中:eO ************* @ TK2MSFTNGP06.phx.gbl ...

这是:


A = B = C;


效率更高:


A = C;

B = C;


在编译的应用程序中?


谢谢。


J
Thanks for the responses. I''m using it to initialize a bunch of properties, or in some cases, clear out a bunch of preprties in a reset function. I like the more compact syntax and I just started wondering if it was any more efficient, or just more convenient.

J
<rl*****@newsgroups.nospamwrote in message news:eO*************@TK2MSFTNGP06.phx.gbl...
Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J


这篇关于这种语法的任何优点......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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