不初始化变量:性能影响 [英] Not Initializing Variables : Performance impact

查看:81
本文介绍了不初始化变量:性能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




来自Microsoft.Performance Code Analysis的警告检查,

不需要将数值变量初始化为零,布尔值为false

和对象为null是一个很好的因为CLR单独完成它。


我想知道是否有人在一个类中初始化字段,多少如果没有完成

的初始化,它会对同样的操作产生什么样的性能。


问候,

Sugandh

Hi,

The warning from Microsoft.Performance Code Analysis check that,
its not required to initialize numeric variables to zero, boolean to false
and object to null is a good one because CLR does it by itself.

I wanted to know if one does initialize fields in a class, how much of the
performance hit would it have for that same operation without a
initialization done.

Regards,
Sugandh

推荐答案

Sugandh,


我无法想象它会对所有。如果你处理常量(比如int i = 1),最多可能是额外的一行
IL。


我真的不会担心这种微优化。

几乎任何其他代码行都肯定比这更有影响。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Sugandh耆那" < su ********** @ nirvana-sol.comwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
Sugandh,

I can''t imagine it is any impact at all. It might be one extra line of
IL at most if you are dealing with constants (like int i = 1).

I really wouldn''t be worried about this kind of micro-optimization.
Pretty much any other line of your code is most definitely going to have
more impact than this.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sugandh Jain" <su**********@nirvana-sol.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...




来自Microsoft.Performance Code Analysis的警告检查,

不需要将数值变量初始化为零,布尔值为false

和对象为空是一个很好的因为CLR单独执行它。

我想知道是否有人在一个类中初始化字段,在没有

初始化的情况下对同一个操作有多少

性能命中完成。


问候,

Sugandh
Hi,

The warning from Microsoft.Performance Code Analysis check that,
its not required to initialize numeric variables to zero, boolean to false
and object to null is a good one because CLR does it by itself.

I wanted to know if one does initialize fields in a class, how much of the
performance hit would it have for that same operation without a
initialization done.

Regards,
Sugandh



4月3,9:53 am,Sugandh Jain < sugandh.j ... @ nirvana-sol.com>

写道:
On Apr 3, 9:53 am, "Sugandh Jain" <sugandh.j...@nirvana-sol.com>
wrote:

来自Microsoft.Performance Code Analysis检查的警告不需要将数值变量初始化为零,boolean为false

和对象为null是一个很好的因为CLR自己完成它。


我想知道如果一个人在一个类中初始化字段,那么在没有
$ b的情况下,同样的操作会有多少

性能命中$ b初始化完成。
The warning from Microsoft.Performance Code Analysis check that,
its not required to initialize numeric variables to zero, boolean to false
and object to null is a good one because CLR does it by itself.

I wanted to know if one does initialize fields in a class, how much of the
performance hit would it have for that same operation without a
initialization done.



好​​吧,CLR会这样做,然后你再次执行相同的操作,

所以虽然它可能没什么影响,执行代码

字面上什么都不做可能不是最干净的代码。

Well, the CLR does it, and then you perform the same operation again,
so while its probably not much of an impact, executing code that
literally does nothing is probably not the cleanest code.


Andy< an *** @ med- associates.com写道:
Andy <an***@med-associates.comwrote:

我想知道是否有人在一个类中初始化字段,
$中有多少如果没有完成

初始化,那么b $ b性能会对同样的操作产生影响。
I wanted to know if one does initialize fields in a class, how much of the
performance hit would it have for that same operation without a
initialization done.



好​​吧,CLR会这样做,然后你再次执行相同的操作,

所以虽然它可能没什么影响,执行代码

字面上什么都不做可能不是最干净的代码。


Well, the CLR does it, and then you perform the same operation again,
so while its probably not much of an impact, executing code that
literally does nothing is probably not the cleanest code.



做一些效率低于它们的东西往往是更方便的代码。
$ br />
我当然看到人们(合理地)争辩说他们会将

成员变量初始化为默认值,专门用来表示他们

期望它在被设置为另一个值之前被读取,留下

他们期望通过

构造函数设置为值的其他变量等。看起来像一个对我来说,这是非常合理的事情。正如Nicholas所说,性能影响可能绝对可以忽略不计。


-

Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet 博客: http:/ /www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

Doing things which are less efficient than they might be is very often
the way to more readable code.

I''ve certainly seen people argue (reasonably) that they''ll initialise a
member variable to its default value specifically to indicate that they
expect it to be read before it may be set to another value, leaving
other variables which they expect to be set to values by the
constructor etc. Seems like a pretty reasonable thing to do, to me. As
Nicholas says, the performance impact is likely to be absolutely
negligible.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于不初始化变量:性能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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