在PHP数组的内存优化 [英] Memory optimization in PHP array

查看:309
本文介绍了在PHP数组的内存优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与大阵这是一个高度图,1024x1024的,当然工作,我是坚持的内存限制。在我的测试机器,我可以增加纪念品限额为1GB,如果我想,但我只有256内存很小VPS,它不是一个选项。

I'm working with a large array which is a height map, 1024x1024 and of course, i'm stuck with the memory limit. In my test machine i can increase the mem limit to 1gb if i want, but in my tiny VPS with only 256 ram, it's not an option.

我一直在寻找在堆栈和谷歌发现了几个好了,你使用的是PHP不是因为记忆效率,沟,并在C ++重写,并坦率地说,没关系,我认识到PHP爱的记忆。

I've been searching in stack and google and found several "well, you are using PHP not because memory efficiency, ditch it and rewrite in c++" and honestly, that's ok and I recognize PHP loves memory.

不过,挖掘更多的PHP内存管理里面的时候,我没有发现什么内存占用每个数据类型。或者,如果转换为另一种类型的数据减少了纪念品消费。

But, when digging more inside PHP memory management, I did not find what memory consumes every data type. Or if casting to another type of data reduces mem consumption.

唯一的优化技术,我发现是尚未设定的变量和数组,仅此而已。

The only "optimization" technique i found was to unset variables and arrays, that's it.

转换的code和C ++使用一些PHP解析器会解决这个问题?

Converting the code to c++ using some PHP parsers would solve the problem?

谢谢!

推荐答案

如果你想有一个真正的数组索引,使用 SplFixedArray 。它使用较少的内存。此外,PHP 5.3具有更好的垃圾收集器。

If you want a real indexed array, use SplFixedArray. It uses less memory. Also, PHP 5.3 has a much better garbage collector.

除此之外,好了,PHP将使用更多的内存比一个更浓墨重彩的C / C ++等价的。

Other than that, well, PHP will use more memory than a more carefully written C/C++ equivalent.

为1024×1024的整数数组内存使用:

Memory Usage for 1024x1024 integer array:


  • 标准数组:218756848

  • SplFixedArray:92914208

测量memory_get_peak_usage()

$array = new SplFixedArray(1024 * 1024); // array();
for ($i = 0; $i < 1024 * 1024; ++$i)
  $array[$i] = 0;

echo memory_get_peak_usage();

请注意,使用64位整数C中的同一阵列是8M。

Note that the same array in C using 64-bit integers would be 8M.

正如其他人的建议,你可以将数据打包成一个字符串。这会慢一些,但的存取效率更高。如果使用8位值是超级简单:

As others have suggested, you could pack the data into a string. This is slower but much more memory efficient. If using 8 bit values it's super easy:

$x = str_repeat(chr(0), 1024*1024);
$x[$i] = chr($v & 0xff); // store value $v into $x[$i]
$v = ord($x[$i]);        // get value $v from $x[$i]

下面内存只能大约1.5MB(即考虑PHP与眼前这个整数字符串数组整个开销时)。

Here the memory will only be about 1.5MB (that is, when considering the entire overhead of PHP with just this integer string array).

有关它的乐趣,我创建创建1024x1024的8位整数,然后通过他们循环一次简单的基准。打包的版本全部采用 ArrayAccess接口,以便用户code看起来都一样。

For the fun of it, I created a simple benchmark of creating 1024x1024 8-bit integers and then looping through them once. The packed versions all used ArrayAccess so that the user code looked the same.

                   mem    write   read
array              218M   0.589s  0.176s
packed array       32.7M  1.85s   1.13s
packed spl array   13.8M  1.91s   1.18s
packed string      1.72M  1.11s   1.08s

打包的阵列使用原生的64位整数(只包装7个字节,以避免处理签名的数据)以及用于包装的字符串 ORD CHR 。显然,实施细节和电脑规格会影响到事情有点,但我希望你得到类似的结果。

The packed arrays used native 64-bit integers (only packing 7 bytes to avoid dealing with signed data) and the packed string used ord and chr. Obviously implementation details and computer specs will affect things a bit, but I would expect you to get similar results.

因此​​,尽管阵列是快6倍,也125X使用内存作为下一个最好的选择:包装字符串。显然,速度,如果你正在运行内存是无关紧要的。 (当我用直接装弦不,他们只是3倍比本地阵列慢的 ArrayAccess接口类。)

So while the array was 6x faster it also used 125x the memory as the next best alternative: packed strings. Obviously the speed is irrelevant if you are running out of memory. (When I used packed strings directly without an ArrayAccess class they were only 3x slower than native arrays.)

总之,要总结,我会用其他的东西比纯PHP如果速度是任何关心的处理这些数据。

In short, to summarize, I would use something other than pure PHP to process this data if speed is of any concern.

这篇关于在PHP数组的内存优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆