• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JoeBoris

Java - Optimizing efficiency/performance

5 posts in this topic

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

0

Share this post


Link to post
Share on other sites
 

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.
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0