Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  efficiency tips!  (Read 2367 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2013-09-19 21:04:20 »

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

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-09-20 17:14:32 »

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


Offline Troncoso

JGO Coder


Medals: 20



« Reply #2 - Posted 2013-09-20 17:17:04 »

What's your alternatives for Lists then? They don't accept primitives.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 49



« Reply #3 - Posted 2013-09-20 17:32:58 »

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.

Mathias - I Know What [you] Did Last Summer!
Offline Troncoso

JGO Coder


Medals: 20



« Reply #4 - Posted 2013-09-20 17:39:18 »

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.
Online UprightPath
« Reply #5 - Posted 2013-09-20 17:41:59 »

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

I'll agree (slightly) here. Only use them when you have to. Especially for Integer, Float, etc.. And try not to do assignments like:
1  
2  
3  
4  
5  
6  
7  
8  
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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2013-09-20 17:43:33 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #7 - Posted 2013-09-20 17:49:43 »

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #8 - Posted 2013-09-20 17:54:34 »

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


"Experience is what you get when you did not get what you wanted"
Offline HeroesGraveDev

JGO Kernel


Medals: 254
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #9 - Posted 2013-09-20 19:05:04 »

Don't use Java2D.

Well somebody had to post it!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #10 - Posted 2013-09-20 20:53:10 »

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!

Offline Jeremy
« Reply #11 - Posted 2013-09-21 04:20:22 »

Don't use Java2D.

Well somebody had to post it!

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.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline kpars

JGO Wizard


Medals: 79
Projects: 4
Exp: 3 years


Extreme Typist.


« Reply #12 - Posted 2013-09-21 05:50:13 »

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.

Offline pitbuller
« Reply #13 - Posted 2013-09-21 09:04:04 »

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.

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.
Offline Danny02
« Reply #14 - Posted 2013-09-21 09:47:50 »

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.

Offline phu004

JGO Coder


Medals: 4
Projects: 9
Exp: 10 years


NoSuchPersonException


« Reply #15 - Posted 2013-09-21 13:32:15 »

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.
Offline lcass
« Reply #16 - Posted 2013-09-21 15:05:03 »

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)
Offline davedes
« Reply #17 - Posted 2013-09-21 17:43:26 »

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.

Offline StumpyStrust
« Reply #18 - Posted 2013-09-22 07:23:39 »

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.

Offline SHC
« Reply #19 - Posted 2013-09-22 09:24:04 »

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

Offline lcass
« Reply #20 - Posted 2013-09-22 17:15:00 »

Ok im gonna change the subject here. Lets do some common exception fixes!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!