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.
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.
That is one of the reasons why the JVM is so blazing fast. 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):
visualvmand its plugins.topto check that.Especially if you do have a lot of data, variables, particles as you say, check first for the obvious things 🙂
Good luck!