• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Some performance questions.

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I'm making an Android Game



I'm still really young programmer, without big experience, so I would like to ask couple of questions (not related to making game, just some Java questions)

Game run's smooth, but I think still there are ways to improve performance (I think its always good to improve code if possible)

I decided to improve code responsible for handling events after collisions/contact between objects.

a) every game object has its ID, like for example "Wall", "Monster", "Coin" so I can know which action should be handled after contact

I have "contact listener" which is checking ID of the two object's which collided, and handling objects, more or less it looks like this (plenty of if checks)



1. So I'm comparing STRING objects, so I thought that comparing int should be faster (or I'm wrong?)

2. Second question, in android development, I'm forced to avoid creating new objects, so I should create all objects at game start up, to avoid garbage collecting which may cause slow downs, and I'm wondering if in previous code, its better to use x1.getBody().getUserData() method tree times (Which returns ID) or create temporary

String s = x1.getBody().getUserData();

and use this s object in those checks ?

3. Last question, since I will switch from using STRING to INT in game objects id, i thought that I should use switch statement, instead of multiple if's. I have been reading that it should be faster.

Thanks for answer.

 
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These considerations are probably not going to speed up your program as much as you think or hope. The best way to write a correct and fast program, is to write clean simple code, and then make tweaks when you find it's not running fast enough.

To me it's silly to handle all these actions in one big handler. It results in big static code. What if a 'Body' has a method like touch(Hero hero), and then Monsters, Coins and Ground can affect the Hero in a polymorphic way? Your listener would become very simple: x1.getBody().touch(steve);
 
Mathew Mintalm
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply, yes I know, I won't see any difference, because of really small amount of those checks, but its always better to use better ways, especially while practising, thats why I asked.
 
Stephan van Hulst
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First worry about nice simple code. If a method deteriorates in a long list of checks, you probably should reconsider your setup. Worry about performance later.
 
Stephan van Hulst
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S., are the graphics your own? I really like them. The spiky monster thing reminds me of Wario Land.
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Graphics are pretty good.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for good words, glad you like it, yes I do own it, its my and my cooperate mate work.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathew Mintalm wrote:1. So I'm comparing STRING objects, so I thought that comparing int should be faster (or I'm wrong?)


Actually, you should probably be comparing types (eg, x1 instanceof Monster); although, as Stephan says, if you write it correctly, each type could possibly deal with its own collisions polymorphically, and so not require any comparisons at all.

2. Second question, in android development, I'm forced to avoid creating new objects, so I should create all objects at game start up, to avoid garbage collecting which may cause slow downs, and I'm wondering if in previous code, its better to use x1.getBody().getUserData() method tree times (Which returns ID) or create temporary

String s = x1.getBody().getUserData();

and use this s object in those checks ?


Sounds better to me (although I'd find a better name than 's') if you still feel you need to do these comparisons at all.

3. Last question, since I will switch from using STRING to INT in game objects id, i thought that I should use switch statement, instead of multiple if's. I have been reading that it should be faster.


It will be, but it may also be more confusing for the reader. It's also worth noting that Java 7 allows switch statements on Strings. However, again, I'd look into ways of avoiding this dispatch code altogether.

That said, I'm no GUI expert (and CERTAINLY no Android expert ).

Winston
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic