简体   繁体   English

使用其他类中的变量比同一类中的变量慢吗?

[英]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: 这是我的建议:

  • Step 1: Find a way to measure the difference. 第1步:找到衡量差异的方法。
  • Step 2: Measure it 第2步:测量它
  • Step 3: Take action based on your measurement 第3步:根据您的测量结果采取措施

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: 这也是人们告诉你以下内容的原因之一:

  • do not micro-optimize Java code for performance, because you are assuming something. 不要微观优化Java代码的性能,因为你正在假设一些东西。 Try to find evidence, first 首先尝试寻找证据
  • especially avoid the most simple things like inlining methods. 特别是避免像内联方法这样最简单的事情。 Hotspot does all of that already Hotspot已经完成了所有这些工作
  • the simpler and cleaner your code the more the Java hotspot compiler can do for you regarding performance 代码越简单越干净,Java热点编译器就性能就可以为您做的越多

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): 除了增加运行时复杂性的算法中的错误......性能问题的最常见原因(首先检查它们):

  • Running out of heap space (and related: garbage collection forever, high cpu load). 耗尽堆空间(和相关:永远垃圾收集,高CPU负载)。 You can check that with tools like visualvm and its plugins. 您可以使用visualvm及其插件等工具进行检查。
  • Contention: Concurrency issues with threads battling for the same resources. 争用:线程争用相同资源的并发问题。 If you do not have I/O waits and idle cpus this is likely your problem. 如果你没有I / O等待和空闲cpu,这可能是你的问题。 Use tools like top to check that. 使用像top这样的工具来检查。
  • Enough memory configured, but your system does a lot of I/O, either because of swapping (too few physical memory) or because of your software waiting for I/O. 配置了足够的内存,但是您的系统会进行大量的I / O操作,无论是因为交换(物理内存太少)还是因为您的软件都在等待I / O. Check for I/O waits 检查I / O等待

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM