efficiency tips!

Post your game efficiency tips here or ask a question about some code.

Avoid calling new in loops in your game.

Don’t EVER use the object wrappers for the primitives (i.e. java.lang.Integer, java.lang.Boolean, etc).

What’s your alternatives for Lists then? They don’t accept primitives.

havent used this lately, but Trove seems to have all you need: http://trove.starlight-systems.com/
.

otherwise just use arrays. they are faster than you think. even when need to copy large chunks with System.arraycopy.

I use mostly arrays. Just pointing out that these wrapper classes have their uses. And the Java API List classes are fine, especially if you aren’t constantly iterating over them or something.

I’ll agree (slightly) here. Only use them when you have to. Especially for Integer, Float, etc… And try not to do assignments like:


private int intValue;
private float floatValue;
public void setValue(Integer intValue) {
this.intValue = (int) intValue;
}
public Float getFloatValue() {
return floatValue;
}

However, using Boolean isn’t that that. I believe that, unless you explicitly use new Boolean(boolean) that it’s smart enough to use the static Boolean.TRUE and Boolean.FALSE rather that instantiating anything new.

Same for Integer: within a specific range, Integer.valueOf(-n…+n) returns a cached Integer instance.

Same rule as with all software; unless you are required to, NEVER optimise code.

While the optimised code might execute more efficiently, it’ll have reduced your efficiency at writing & maintaining it.

Computers get faster; humans do not.

Exactly. make it easy to read/follow #1

Only ‘optimize’ or care about it, when you are ‘profiling’ and not because you think you are smarter than the compiler.

Most of the time people’s optimization actually may make it worse. Do not rely on your ‘gut’ and intuition. Believe in the profiler! and what actual performance changes occur. when you try to optimize code.

At the very least, run some sample real world conditions to test it ‘new optimization’

For those who have understanding of where you deal with computational time, O(n) for worst case, average case, best case. Just because you optimize for best case, doesn’t mean you don’t make a horrible worst or average case. So be cautious with your ‘samples’ and test conditions

Don’t use Java2D.

Well somebody had to post it!

I remember when Notch was using Integer as a Map key, and he was experiencing some serious performance problems with Minecraft.

Integer.valueOf only returns cached instances between -127 and 127, not that big of a range. If you know it’s between these numbers you might as well use an array as a perfect hash map!

  1. Start developing on a portable platform
  2. Implement native dependancies because the alternative isn’t worth trying.
  3. Why am I using Java again?

Java2D isn’t that bad, you have to be careful with how you use it, and you have to acknowledge there are some serious limitations that are difficult to work around.

You can use Java2D, just make sure you render everything from the GPU rather than the CPU.

Since I don’t have any complex graphics or odd hitboxes for everything, it works like a charm for me.

  • Jev.

While you are on your task you should use at least couple minutes to think if there are any oblivious performance problems in your code. It’s lot faster and easier to modify code that is still fresh in your memory than later. If you only optimize later on. You end up optimizing couple obvious bottleneck and after that profiler will show that 80-90% of time is used for bulk code without any significant code paths left for optimizing if performance is still then problem you need to rewrite most of the code which is just total nightmare.

my tip for optimization is to go from top to bottom. This means first take a look of the complexity of your code big O notation and only after that think about micro optimisations.

My tips are: pick the fastest algorithm, make sure it works and meet your expectation, then optimize the 10% code that get executed 90% of the time.

My tip, where possible do not use new int[][] but rather new int[] you can search through it quicker and can expect where the latest items will be (the end)

Check out LibGDX’s utils:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/package-summary.html

It will lead to generally more optimized code with minimal allocations.

Other than that, try to avoid premature optimization.

Best thing which will often produce better code is to THINK before you code. Draw or write down what classes you will need and the methods they might need. You will never get a perfect model but this will clear out many possible performance issues before any code is even there.

  • Be clear on what you wanted to do.
  • Think about good design structure before you code.
  • Opt a good readable code style.
  • Never do premature optimization.