简体   繁体   中英

Looping : 10,000 arrays (time wasting) vs 1 global array (memory wasting)

For performance reason, I have a game map divided in 10,000 tiles. Each tile has an array of entities. Thus, entities entering/leaving a tile is pushed/removed into/from the corresponding array.

On the other hand, to avoid to loop on the 10,000 arrays to update all the entities every x ms , what is the most efficient way to handle arrays ?

Besides the tiles array, should I create one global array containing all the entities from all the tiles ? Is it not memory wasting ?

This is a common dichotomy in performance tuning, processing vs memory. My answer as always, (especially regarding performance) is "it depends". The only real way to know is to measure both and see if the speed/memory usage fits within some acceptable bounds. eg. If you want 60fps you have to be <16ms per frame, no questions asked.

My suggestion would be to implement it in whichever way is the most logical to the reader (ie. you/your team) and only once it works mangle the code for performance gains (and then, only once you can measure them). This prevents you "optimizing" code and making it unreadable/unmaintainable (or, at least less so) without having evidence it is required and that it is an actual improvement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM