2

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.

6
  • Are you accessing the variables through get methods or are the variables declared public? Commented Aug 6, 2011 at 17:42
  • 1
    Have you actually measured a performance difference based only on the location of the variables? Commented Aug 6, 2011 at 17:45
  • 1
    @Jeffrey: Red herring. Modern JVMs have function inlining and using getters is not slower if all they do is just returning the value of a field. Commented Aug 6, 2011 at 17:46
  • @Jeffrey They are Public. Mehrdad : I like Capitalization too much . Don Roby : Nope, I just Noticed it plus it would be a bit hard to Mesure since the particles Appear when you drag your Mouse Commented Aug 6, 2011 at 17:48
  • @Chris Jester-Young Oh, I didn't know that. But if he isn't using a modern JVM it might have been an issue, but seeing as they're public variables it doesn't matter anyways. :) Commented Aug 6, 2011 at 17:51

2 Answers 2

5

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.
  • Step 2: Measure it
  • Step 3: Take action based on your measurement

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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:

  • do not micro-optimize Java code for performance, because you are assuming something. Try to find evidence, first
  • especially avoid the most simple things like inlining methods. Hotspot does all of that already
  • the simpler and cleaner your code the more the Java hotspot compiler can do for you regarding performance

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). You can check that with tools like visualvm and its plugins.
  • 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. Use tools like top to check that.
  • 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. Check for I/O waits

Especially if you do have a lot of data, variables, particles as you say, check first for the obvious things :-)

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.