General tips to improve performance

Anything libgdx related goes here!

General tips to improve performance

Postby neurocase » Wed Apr 16, 2014 7:50 am

Hi, concerning LibGDX, Box2d and Java;
What are some general tips when programming that will improve performance?
How should code be factored to optimize performance?
What/when should unused datatypes/objects be reused when possible instead of creating new ones?

Thanks :geek:
_Daz
neurocase
 
Posts: 27
Joined: Fri Feb 21, 2014 5:23 am

Re: General tips to improve performance

Postby cryo75 » Wed Apr 16, 2014 8:55 am

- avoid multiple spritebatches or multiple begin/end
- avoid useless calculations in render methods (pre-calculate once beforehand if values will never change)
- avoid converting int/float/etc to string everytime in render
- avoid creating objects (reuse objects)
cryo75
 
Posts: 403
Joined: Thu Nov 22, 2012 7:41 am

Re: General tips to improve performance

Postby Magnesus » Wed Apr 16, 2014 9:03 am

- avoid destroying objects until the game finishes
- use culling - make sure you don't draw outside of screen
- don't overuse semitransparency - it tends to be slow
- consider not updating any objects that are out of screen (for example don't animate things that are outside of the screen until they are visible, very important for Spine2D)
- keep aware that creating and adding strings can cause garbage collector to act (causing lags) - so don't do that too often in game loop
Magnesus
 
Posts: 1523
Joined: Sun Sep 25, 2011 3:50 pm

Re: General tips to improve performance

Postby evilentity » Wed Apr 16, 2014 9:30 am

profile before you optimize
Need a freelancer? PM me!
evilentity
 
Posts: 3582
Joined: Wed Aug 24, 2011 11:37 am

Re: General tips to improve performance

Postby neurocase » Wed Apr 16, 2014 10:51 am

Ahh thanks, I just read this https://code.google.com/p/libgdx/wiki/MemoryManagment , I'll spend sometime tomorrow converting objects I've just been creating and destroying like bullets and enemies and put them into a pool. That should solve the majority of my problems.
neurocase
 
Posts: 27
Joined: Fri Feb 21, 2014 5:23 am

Re: General tips to improve performance

Postby davebaol » Wed Apr 16, 2014 11:45 am

Also avoid the foreach construct
Code: Select all
for (Item item : collection) {
    ....
}

because an iterator is implicitly created.
davebaol
 
Posts: 182
Joined: Tue Apr 08, 2014 7:08 am

Re: General tips to improve performance

Postby jrenner » Wed Apr 16, 2014 1:20 pm

davebaol wrote:Also avoid the foreach construct
Code: Select all
for (Item item : collection) {
    ....
}

because an iterator is implicitly created.


You can do that with gdx array, but not with Arraylists, etc
Superior Tactics - tactical spaceship battles RTS
Google Play: https://play.google.com/store/apps/deta ... r.superior
jrenner
 
Posts: 520
Joined: Sun Apr 14, 2013 5:09 am

Re: General tips to improve performance

Postby davebaol » Wed Apr 16, 2014 1:50 pm

jrenner wrote:
davebaol wrote:Also avoid the foreach construct
Code: Select all
for (Item item : collection) {
    ....
}

because an iterator is implicitly created.


You can do that with gdx array, but not with Arraylists, etc

Yeah but then you should mention that the same iterator instance is shared by all foreach constructs over the same array (actually each call to Array.iterator()), meaning that nested and mulithread iterations are seriously affected. ;)
davebaol
 
Posts: 182
Joined: Tue Apr 08, 2014 7:08 am

Re: General tips to improve performance

Postby Magnesus » Wed Apr 16, 2014 2:53 pm

davebaol wrote:Yeah but then you should mention that the same iterator instance is shared by all foreach constructs over the same array (actually each call to Array.iterator()), meaning that nested and mulithread iterations are seriously affected. ;)


Ha, that explains some problems I had with libGDX arrays. :)
Magnesus
 
Posts: 1523
Joined: Sun Sep 25, 2011 3:50 pm

Re: General tips to improve performance

Postby davebaol » Wed Apr 16, 2014 3:37 pm

Yep java programmers are used to get always a new iterator by any implementation of Iterable, but libgdx team decided to break the usual behavior for performance reasons I guess. This is ok though, since the Iterable documentation doesn't impose this constraint and Array's documentation explicitly states this unusual behavior.
davebaol
 
Posts: 182
Joined: Tue Apr 08, 2014 7:08 am

Next

Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot] and 11 guests