难道真的SplFixedArray执行比数组更好吗? [英] Does really SplFixedArray perform better than arrays?

查看:203
本文介绍了难道真的SplFixedArray执行比数组更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了SplFixedArray建设有一周的日子数组,我得到下面的结果:

I'm testing the SplFixedArray building an array with the days of the week, and I get the following results:

<?php

$days = new SplFixedArray(7);

$days[0] = "Monday";
$days[1] = "Tuesday";
$days[2] = "Wednesday";
$days[3] = "Thursday";
$days[4] = "Friday";
$days[5] = "Saturday";
$days[6] = "Sunday";

echo memory_get_peak_usage() . "\n"; //Returns 327688
echo memory_get_usage() . "\n"; //Returns 327140
echo memory_get_peak_usage(true) . "\n"; //Returns 524288
echo memory_get_usage(true) . "\n"; //Returns 524288 

使用传统阵列:

<?php

$days = array();

$days[0] = "Monday";
$days[1] = "Tuesday";
$days[2] = "Wednesday";
$days[3] = "Thursday";
$days[4] = "Friday";
$days[5] = "Saturday";
$days[6] = "Sunday";

echo memory_get_peak_usage() . "\n"; //Returns 327528
echo memory_get_usage() . "\n"; //Returns 326820
echo memory_get_peak_usage(true) . "\n"; //Returns 524288
echo memory_get_usage(true) . "\n"; //Returns 524288

这会让你感觉?

推荐答案

如图所示通过的这篇文章

我们可以得出这样的结论SplFixedArray的内存占用量确实较小,但明显只针对大量数组元素的。由于SplFixedArray技术上一个类的实例藏汉,相对于传统阵列,这是什么原因造成小数组为实际悦目多个存储器如果SplFixedArray实施耗时,但作为字节这个额外几百保持恒定,如在阵列的大小增长变得无关紧要。

One can conclude that the memory footprint of SplFixedArray is indeed smaller, but noticeable only for a large amount of array elements. Because SplFixedArray is technically an instance of a class aswell, as opposed to traditional arrays, that is what is causing small arrays to be actually sightly more memory consuming if implemented by SplFixedArray, but as this extra few hundred of bytes remains constant, it becomes irrelevant as the size of the array grows.

边注:不要微优化,而不是针对每一个钉子创建的每个锤子。 SplFixedArray是有极端的情况下,例如对于成千上万的元素,其中削减每个元素内存占用几个字节的阵列对整个内存使用量有很大的影响;但也懒得使用它,除非你真的确定你的数组是或可能是应用程序的一个潜在的瓶颈。

Side note: don't micro-optimize, not every hammer is created for every nail. SplFixedArray is there for extreme cases, e.g. for arrays of hundreds of thousands of elements, where cutting down a few bytes of memory usage per element has a large impact on the overall memory usage; but don't bother using it unless you are really sure your array is or could be a potential bottleneck of the application.

这篇关于难道真的SplFixedArray执行比数组更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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