Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Java - Optimizing efficiency/performance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 J03_b   Members   -  Reputation: 131

Like
0Likes
Like

Posted 05 July 2013 - 12:57 AM

So I'm writing my first game in Java, and I'm following a tutorial series on Youtube.  I've come to a point where I'm going to start cleaning up the code before I move on.  One thing the tutorial says to do that doesn't seem very wise is to declare new variables in the tick( ) and render( ) methods that run dozens of times per second.  Wouldn't using class level variables in the tick and render functions be faster because the program can skip the creation and deletion of said variables.

 

 

Another thing that I was going to do was encapsulate all of my class level variables.  My college professor said it is a good practice and he actually made us do it.  Eventually I learned to like the consistency of only using get/set methods when accessing class level variables.  However, in game programming, I've had lots of variables so far that are never accessed outside the class.  Should I only make get/set methods for variables accessed outside the class?  Do get/set methods hurt performance at all?

 

Thanks



Sponsor:

#2 LorenzoGatti   Crossbones+   -  Reputation: 2779

Like
0Likes
Like

Posted 05 July 2013 - 02:07 AM

 

One thing the tutorial says to do that doesn't seem very wise is to declare new variables in the tick( ) and render( ) methods that run dozens of times per second.  Wouldn't using class level variables in the tick and render functions be faster because the program can skip the creation and deletion of said variables.

Keep some perspective: dozens of times per second is negligible.
Moreover, any reasonable JVM is going to recycle memory for stack-allocated objects as efficiently as holding onto them with class variables.
You should declare variables where they make sense.

However, in game programming, I've had lots of variables so far that are never accessed outside the class. Should I only make get/set methods for variables accessed outside the class?

 
Yes, that's the whole point.
But maybe the appropriate way to interact with your object is more abstract than getting and setting attributes; for example, a method to order a guided missile to follow a certain object instead of a setter for its velocity allows the missile to guarantee the integrity of its own steering.
Produci, consuma, crepa

#3 BitMaster   Crossbones+   -  Reputation: 4437

Like
0Likes
Like

Posted 05 July 2013 - 02:18 AM

One thing the tutorial says to do that doesn't seem very wise is to declare new variables in the tick( ) and render( ) methods that run dozens of times per second.  Wouldn't using class level variables in the tick and render functions be faster because the program can skip the creation and deletion of said variables.

That depends on what happens there. If they are primitive types (like int, float, ...) then they will be allocated on the stack. That is extremely fast to do and will most likely be faster than accessing member variables.
If they are new'ed then it will largely depend on what exactly happens inside the allocated object. If the objects can be reasonably recycled (like for example something like a Vector3f) then it's probable having an instance around as a member variable could be faster.
 

Another thing that I was going to do was encapsulate all of my class level variables.  My college professor said it is a good practice and he actually made us do it.  Eventually I learned to like the consistency of only using get/set methods when accessing class level variables.  However, in game programming, I've had lots of variables so far that are never accessed outside the class.  Should I only make get/set methods for variables accessed outside the class?  Do get/set methods hurt performance at all?

If you end up with one getter and one setter for each member variable and each of those are just of the form "variable = parameter;"/"return variable;" then you are producing a lot of boiler plate code for nothing. It also means you are not really making yourself independent of the class's internal implementation which is usually the point of getters/mutators.
A decent Java runtime will probably be able to eliminate the getter/setter call during JIT compilation but the underlying problem remains. My personal heuristic would be:
(1) the variable should not be directly writable from outside: having a getter method and no setter function makes sense.
(2) modifying the value also requires changing more state (for example setting a dirty-flag): having a setter makes sense, having a getter is optional depending on the need.
(3) although (2) is currently not an issue it could reasonably become an issue in the future.
As usual, there exist a lot of exceptions and corner cases for these rules. But if you cannot immediately point to one of them before implementing the getter/setters, you should spend a bit of time thinking if you really need it or if it's just a band-aid for an underlying encapsulation problem.

#4 markr   Crossbones+   -  Reputation: 1653

Like
0Likes
Like

Posted 05 July 2013 - 07:08 AM

You really need to profile your code to find out where the time is being spent.

 

It is true that if you make too much work for the GC, it will slow your game down. This might be a problem. Also maybe a problem will be "pauses" while the GC does its work. Hopefully none of this happens too much any more.

 

Regarding get/set - no VM created in the last 10 years should see any performance drop for get/set once it has compiled the class with the JIT. The JIT can inline any method it wants to (yes - even if it's NOT declared "final" - the JIT has information about every class loaded and knows whether a method is overridden anywhere) and will hopefully do so for a setter / getter.



#5 Sporniket   Members   -  Reputation: 350

Like
0Likes
Like

Posted 05 July 2013 - 07:27 AM


Another thing that I was going to do was encapsulate all of my class level variables. My college professor said it is a good practice and he actually made us do it. Eventually I learned to like the consistency of only using get/set methods when accessing class level variables. However, in game programming, I've had lots of variables so far that are never accessed outside the class. Should I only make get/set methods for variables accessed outside the class? Do get/set methods hurt performance at all?

 

The point of this best practice is to be able to refactor the properties without breaking : for now, the getFoo/setFoo is just reading/writing the field named "foo", but maybe in the futur it will be the result of a computation of some other fields. So this is valid even for field processed only in the internals of your class.

So, if you don't expect your code to need refactoring for some fields, or if you don't plan to maintain your code, you may not follow this practice.

 

On the performance side, there is a performance hit compared to a direct access. Is it a serious hit compared to other things ? Use a profiler for evaluating that.


Space Zig-Zag, a casual game of skill for Android by Sporniket-Studio.com


#6 Bubsy   Members   -  Reputation: 407

Like
0Likes
Like

Posted 05 July 2013 - 08:10 AM

Profile and play with garbage collector settings and VM's optimization options.

 

The newer GC's algorithms are optimized to deal with short lived object. There is also the escape analysis optimization, which was incorporated to the newer Java versions. The escape analysis can, by analysing the code in runtime, determine if the created object will be allocated on the heap or could be allocated on the "stack".

 

More info at http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS