Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-存储数据的数组或对象-什么是更好的解决方案_Php_Arrays_Performance_Oop - Fatal编程技术网

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

php-存储数据的数组或对象-什么是更好的解决方案,php,arrays,performance,oop,Php,Arrays,Performance,Oop,我只是在想什么能让我更好的表现和内存使用。我正在为一个游戏编写地图。所有内容都存储在类“map”中,但map可能有数千个分幅。现在我在想什么是更好的解决方案: 将每个磁贴参数保留在x-y数组中(磁贴x=10,y=11数据存储在像map[10][11]=params这样的数组中),如果我想了解有关磁贴的信息,请使用x-y参数调用不同的数组 将一个磁贴视为一个对象并生成方法,它可以轻松地返回磁贴的邻居以及作为数组存储在每个对象中的所有数据 因为我可能有数千个类似的块,第一印象是它最适合oop编程

我只是在想什么能让我更好的表现和内存使用。我正在为一个游戏编写地图。所有内容都存储在类“map”中,但map可能有数千个分幅。现在我在想什么是更好的解决方案:

  • 将每个磁贴参数保留在x-y数组中(磁贴x=10,y=11数据存储在像map[10][11]=params这样的数组中),如果我想了解有关磁贴的信息,请使用x-y参数调用不同的数组
  • 将一个磁贴视为一个对象并生成方法,它可以轻松地返回磁贴的邻居以及作为数组存储在每个对象中的所有数据
因为我可能有数千个类似的块,第一印象是它最适合oop编程,但我担心数千个对象块可能会占用更多内存,调用它的方法可能会更慢。你觉得怎么样


Kalreg

如果您有一个分片网格,它非常适合存储在阵列中

但是,如果这些瓷砖上附着了大量信息,那么这些信息就适用于对象


我建议使用对象数组。

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

检查此阵列与Aron Novak的对象比较/测试:。 我用阿隆·诺瓦克斯的话说:

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.

查看这篇关于PHP数组与对象内存使用和性能的详细/优秀文章

您正在开发一款游戏,您肯定不能只使用
数组
对象
,您很可能同时使用这两种

我想你应该先明白


SplFixedArray
替换您的数组,并将对象存储在
SplObjectStorage
中。您的代码应该有所改进。

完美的“为什么不同时使用两者”解决方案。数组以便于访问,对象以便于使用。您使用的是哪个php版本?我使用的php版本为5.3,可能是的副本。很高兴答案对您有所帮助。不客气:)