[英]Is using variables from other classes slower than variables within the same class?
I'm making a game, and I recently organized all the code. 我正在制作游戏,最近我组织了所有代码。 For drawing, I now use variables from another class, and I noticed a quite important performance decrease at the time of the reorganization. 对于绘图,我现在使用来自另一个类的变量,并且我注意到在重组时性能的下降非常重要。
So I'm wondering: Is accessing variables in another class slower than accessing variables within the same class? 所以我想知道:访问另一个类中的变量比访问同一个类中的变量要慢吗?
Note: I have a very big number and different particles to draw, so a lot of variables to access. 注意:我有一个非常大的数字和不同的粒子来绘制,所以要访问很多变量。
It's really hard to answer helpfully without specific info on your computer environment and your specific code. 如果没有关于您的计算机环境和特定代码的具体信息,很难回答。 Here's what I suggest instead: 这是我的建议:
Note: if you can't measure the difference, but are just going by 'feeling' you'll never really know if this problem is real or imagined. 注意:如果你无法衡量差异,但只是“感觉”,你永远不会真正知道这个问题是真实的还是想象的。
Usually reading fields in different objects even through methods does not make any difference. 通常,即使通过方法读取不同对象中的字段也没有任何区别。 As soon as your load goes up on a specific 'hot spot' the Java hotspot compiler optimizes the bytecode on-the-fly so that it won't make a difference anymore. 只要您的负载在特定的“热点”上升,Java热点编译器就会即时优化字节码,这样它就不再有用了。
That is one of the reasons why the JVM is so blazing fast. 这就是JVM如此快速发展的原因之一。 And it is one of the reasons why people tell you the following: 这也是人们告诉你以下内容的原因之一:
If you notice performance issues, always check for memory issues first. 如果您发现性能问题,请始终先检查内存问题。 Aside from bugs in algorithms that increase runtime complexity... The most common reasons for performance issues (check for them first): 除了增加运行时复杂性的算法中的错误......性能问题的最常见原因(首先检查它们):
visualvm
and its plugins. 您可以使用visualvm
及其插件等工具进行检查。 top
to check that. 使用像top
这样的工具来检查。 Especially if you do have a lot of data, variables, particles as you say, check first for the obvious things :-) 特别是如果您确实拥有大量数据,变量,粒子,请先检查明显的事情:-)
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.