php – 用于存储数据的数组或对象 – 什么是更好的解决方案

我只是在想什么会给我更好的性能和内存使用.我正在编写一个游戏地图.一切都存储在“地图”类中,但地图可能有数千个图块.现在我在想什么是更好的解决方案:

>将每个tile参数保存在x-y数组中(tile x = 10,y = 11数据存储在数组中,如map [10] [11] = params)如果我想知道某个tile的某些内容,则调用带有x-y参数的不同数组
>将tile视为对象并制作方法,它可以轻松返回tile的邻居,并且所有数据也作为数组存储在每个对象中.

因为我可能有数千个类似的瓷砖,第一印象是它最适合oop编程,但我担心成千上万的对象图块可能会使用更多的内存并且调用它的方法可能会更慢.你怎么看待这件事?

Kalreg

解决方法:

这取决于您使用的PHP版本.如果您使用的版本是PHP5或更高版本(您应该将最新的PHP版本视为游戏应用程序的更高性能),那么使用数组和对象将为您的应用程序提供几乎相同的性能.因此,如果您正在使用/可以使用PHP 5或更高版本,那么您可以根据自己的方便使用数组或对象.

检查此数组与对象比较/测试由Aron Novak:http://aggregation.novaak.net/?q=node/227.
我在Aron Novaks的话中引用它:

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.

请查看此http://we-love-php.blogspot.com/2012/06/php-memory-consumption-with-arrays.html获取有关PHP数组与对象内存使用和性能的详细/精彩文章

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...