局部变量与实例变量 [英] Local variables vs instance variables

查看:84
本文介绍了局部变量与实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为使用XNA构建的游戏进行C#优化方面的大量研究,但我仍然不太了解局部变量是否是实例变量,当不断更新和使用它们时,它们能提供更好的性能.

I've been doing a lot of research on C# optimization for a game that I'm building with XNA, and I still don't quite understand whether local variables are instance variables give better performance when constantly being updated and used.

根据 http://www.dotnetperls.com/optimization ,应避免使用参数和局部变量变量,这意味着实例变量是性能方面的最佳选择.

According to http://www.dotnetperls.com/optimization , you should avoid parameters and local variables, meaning instance variables are the best option in terms of performance.

但是不久前,我在另一篇StackOverflow文章中读到(我似乎找不到它在哪里),局部变量存储在内存中,它的访问速度要快得多,并且每次实例变量设置后,必须先擦除以前的值,这是一个繁琐的额外步骤,然后才能分配新值.

But a while ago, I read on another StackOverflow post (I can't seem to find where it was) that local variables are stored in a part of memory that is far quicker to access, and that every time an instance variable is set, the previous value has to be erased as a tedious extra step before a new value can be assigned.

我知道在设计方面,在这种情况下使用实例变量可能会破坏封装,但是我对性能完全感到好奇.目前,在我的游戏中,我将局部变量传递给类中的7种方法中的3种,但是我可以轻松地将变量提升为实例变量,并能够完全避免参数传递和局部变量.

I know that design-wise, it might break encapsulation to use instance variables in that kind of situation, but I'm strictly curious about performance. Currently in my game, I pass around local variables to 3 out of 7 methods in a class, but I could easily promote the variables to instance variables and be able to entirely avoid parameter passing and local variables.

那哪个更好?

推荐答案

我个人不会将其视为性能问题的罪魁祸首(除非您不断传递大型 struct s).我天真的理解是,GC压力是XNA游戏中通常要考虑的问题,因此基本上节约了对象实例.

Personally, I wouldn't be looking at this as the culprit for performance issues (unless you are constantly passing large structs). My naive understanding is that GC pressure is the usual consideration with XNA games, so being frugal with your object instances basically.

如果变量是方法局部变量,则值本身或引用(当引用类型时)将位于堆栈上.如果将这些变量提升为类成员变量,它们将位于堆上类的内存中.

If the variable is method-local, the value itself or the reference (when a reference type) will be located on the stack. If you promote those to class member variables they will be located in the class's memory on the heap.

从技术上讲,方法调用将变得更快,因为您不再在调用中复制引用或值(因为如果该方法对于该类也是本地的,则可以从方法中删除参数).

Method calls would technically become faster as you are no longer copying references or values on the call (because presumably you can remove the parameters from the method if the method is also local to the class).

我不确定相对性能,但是对我来说,如果您需要保留该值,则该值在类中是有意义的...

I'm not sure about the relative performance, but to me it seems that if you need to persist the value then the value makes some sense being in the class...

在我看来,两者之间的细微差异似乎抵消了以一方优势支持另一方的任何潜在收益-使二者之间的差异大致相等或相差很小,以至于不在乎.

To me it seems like any potential gains from doing one in favour of the other is outweighed by the subtle differences between the two - making them roughly equivalent or so small a difference as to not care.

当然,面对性能分析中的困难数字,所有这些问题都可以得到纠正.

Of course, all this stands to be corrected in the face of hard numbers from performance profiling.

这篇关于局部变量与实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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