3

I am just thinking what would give me better perfomance and memory usage. I am programming a map for a game. Everything is stored in class "map" but a map may have thousands of tiles. Now i am thinking what is better solution:

  • keep every tile parameters in x-y array (tile x = 10, y = 11 data is stored in array like map[10][11] = params) and if i want to know something about a tile a call different arrays with x-y parameters
  • consider a tile as an object and make methods to, it can easily return tile's neighbours and all data also stored somehere in each object as an array.

Because I may have thousands of similiar tiles first impression would be that it is best for oop programming, but i am afraid that thousands of object tiles might use much more memory and calling to it's methods might be slower. What do you think about it?

Kalreg

3
  • which php version are you using? Commented Mar 28, 2013 at 17:51
  • i use php in version 5.3 Commented Mar 28, 2013 at 19:42
  • possible duplicate of PHP Objects vs Arrays Commented Apr 3, 2013 at 17:11

3 Answers 3

4

It depends on the php version you are using. If the version you are using is PHP5 or above(which you should as latest php version will mean more performance for your game app) then using both arrays and objects will give almost same performance for your application. So if you are-using/can-use PHP 5 or above then you may use arrays or objects according to your convenience.

Check this array vs object comparison/tests by Aron Novak: http://aggregation.novaak.net/?q=node/227. I am quoting it bellow in Aron Novaks words:

I created a small script to find out which data structure has better performance. You can find the script what I used for the test at the attachments.
My test results are:
    data structure          PHP4        PHP5
    object                  61.6224     37.576
    array                   57.6644     37.866

    The results are in milliseconds(ms). It seems that in PHP5 the data structure is totally indifferent, in PHP4 there is approximately 7% performance gain if use arrays instead of objects.
    "This revolution, however, left PHP's object model mostly unchanged from version 3 – it was still very simple. Objects were still very much syntactic sugar for associative arrays, and didn't offer users too many features on top of that." (http://devzone.zend.com/node/view/id/1717)
    So the syntactic sugar is not a bad thing at all :) And it costs almost nothing. This is an explanation why all the parsers' output uses objects.

Check this http://we-love-php.blogspot.com/2012/06/php-memory-consumption-with-arrays.html for a detailed/great article on PHP arrays vs objects memory Usage and Performance

Sign up to request clarification or add additional context in comments.

1 Comment

I am glad the answer helped you. You are Welcome :)
1

You are developing a game definitely you can not use only array or objects you are mostly likely to use both.

I think you should first understand How big are PHP arrays (and values) really? (Hint: BIG!)

Replace your array with SplFixedArray and store your objects in SplObjectStorage you should get improvements in your code.

Comments

0

If you have a grid of tiles, that lends itself really well to being stored in an array.

But if those tiles have a lot of information attached to them, that lends itself to objects.

I would suggest an array of objects.

1 Comment

The perfect "why not both" solution. Array for ease of access, object for ease of usage.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.