Jump to content
  • Advertisement
Sign in to follow this  
menyo

Best optimization practises

This topic is 2498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently i'm optimizing my tile class and i'm wondering about a couple of things. Each tile goes into a array to eventually draw the map.

I have a lot of fields declared in this tile class that obviously take memory. Things like:
tile position relative to the grid.
absolute tile position.
absolute center of the tile.

1. Since i can calculate the absolute tile position with a simple method and call that only when needed i'm wondering if that would be more efficient. Obviously if i call this method a lot it will be more expensive since it needs to calculate it each time. But there will be lots of tiles in this array and only on a handful of tiles i need to check it's center for instance.

I could also move the method to get this absolute position from this tile class to some kind of helper class. Since i just need the grid coordinates times the tilesize i could make this a static method.

2. Also, i have read a "new" statement is rather expensive. Up till now i often used this for instance in my draw and update loops. Like when i want to draw on position i just put "new Vector2(absPosX,absPosY)". Where i can just make this vector2 in the constructor of the tile class andpass that into the overload. I reckon it's better to declare a field for this since it's needed every draw loop.

Thanks,

Share this post


Link to post
Share on other sites
Advertisement
How sparse is your tile grid? When you say the tiles are stored in an array, can you be more specific? Perhaps the tile doesn't need to know its own position if it is implicit from its location in the array.


1. Since i can calculate the absolute tile position with a simple method and call that only when needed i'm wondering if that would be more efficient.


You asked about best-practices for optimisation. In general, the advice is:

  • Think about which algorithms and data structures are appropriate when developing, but don't get bogged down attempting to optimize everything ahead of time
  • If there is a performance issue, measure to find out which parts of the program could benefit from optimization
  • Once you have identified "hot spots", measure again to see which alternative way of doing things is actually more efficient


    In reality, there are often so many factors involved that a "rule of thumb" can be a dangerous thing. The decision about a particular trade-off between memory and CPU, for example, will depend on the platform(s) your game runs on and the properties of the game itself (e.g. how frequently a piece of information is required and how often it changes).

    That said, I will suggest a rule of thumb: Generally game entities should care about their position in the game world, not their on-screen position or how they are rendered.



    [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

    2. Also, i have read a "new" statement is rather expensive.

    [/font]


    [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

    [/quote]

    [/font]

    • [font="helvetica, arial, verdana, tahoma, sans-serif"][color="#282828"]Your reaction to reading something like this shouldn't be to immediately attempt to eliminate "new"s from your programs. It may be that you notice an actual performance issue at some point and this gives you an idea for a potential optimization.[/font]
    • [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]There will be places where using "new" is absolutely the right thing to do - [/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]in fact there will probably be many, as allocating memory is something fundamental to computer programs[/font]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!