简体   繁体   English

SplFixedArray 的性能真的比数组好吗?

[英]Does really SplFixedArray perform better than arrays?

I'm testing the SplFixedArray building an array with the days of the week, and I get the following results:我正在测试 SplFixedArray 用星期几构建一个数组,我得到以下结果:

<?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 

With traditional arrays:使用传统数组:

<?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

Does it make sense for you?这对你有意义吗?

As illustrated by the benchmarks performed by the author of this article :正如本文作者执行的基准测试所示:

http://www.johnciacia.com/wp-content/uploads/2011/01/3.png

One can conclude that the memory footprint of SplFixedArray is indeed smaller, but noticeable only for a large amount of array elements .可以得出结论,SplFixedArray 的内存占用确实较小,但仅在大量数组元素时才会引起注意 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 在技术上也是一个类的实例,而不是传统数组,这就是导致小数组实际上在 SplFixedArray 实现时消耗更多内存的原因,但由于这额外的几百字节保持不变,它变得无关紧要随着数组大小的增长。

Side note: don't micro-optimize, not every hammer is created for every nail.旁注:不要进行微观优化,并非每个锤子都是为每个钉子创建的。 SplFixedArray is there for extreme cases, eg 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; SplFixedArray 适用于极端情况,例如,对于包含数十万个元素的数组,在这种情况下,将每个元素的内存使用量减少几个字节会对整体内存使用量产生很大影响; but don't bother using it unless you are really sure your array is or could be a potential bottleneck of the application.但不要费心使用它,除非你真的确定你的阵列是或可能是应用程序的潜在瓶颈。

A SplFixedArray is supposed to be faster than arrays. SplFixedArray 应该比数组更快。 It doesn't say anything about memory consumption (which is what you're testing here).它没有说明内存消耗(这是您在此处测试的内容)。 From http://php.net/manual/en/class.splfixedarray.php :来自http://php.net/manual/en/class.splfixedarray.php

"The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation" “SplFixedArray 和普通 PHP 数组之间的主要区别是 SplFixedArray 是固定长度的,并且只允许范围内的整数作为索引。优点是它允许更快的数组实现”

However, using an array of 100000 entries, reveals that it also uses less RAM:然而,使用一个包含 100000 个条目的数组,表明它也使用了更少的 RAM:

$users = array();
for ($i=0;$i<100000;$i++) { 
    $users[$i] = array('id' => rand(), 'name' => 'default');
}
echo memory_get_peak_usage(true); //returns 31457280

$users = new SplFixedArray(100000);
for ($i=0;$i<100000;$i++) { 
    $users[$i] = array('id' => rand(),
            'name' => 'default');
}
echo memory_get_peak_usage(true); //return 26738688

yes if you are using them with a fixed size.是的,如果您使用固定尺寸的它们。

If you are constantly changing the size for each new element to add could be slower and it it might not be the wrong usage too.如果您不断更改要添加的每个新元素的大小,则可能会变慢,而且这也可能不是错误的用法。

It is faster due to the implementation of array in PHP that those are not real array as a per definition in programming languages, but are instead associative array, implemented with a Hash table.由于在 PHP 中实现了数组,因此速度更快,因为根据编程语言的定义,这些数组不是真正的数组,而是关联数组,使用哈希表实现。 (so array in PHP are basically Hash Tables) (所以 PHP 中的数组基本上是哈希表)

Whilst the SplFixedArray is implemented using the "malloc" of C as a almost normal C array, of course wrapped in a small struct to keep track and manipulate the array as a per use cases.虽然 SplFixedArray 是使用 C 的“malloc”实现的,作为一个几乎正常的 C 数组,当然包装在一个小结构中以根据用例跟踪和操作数组。

UPDATE更新

At the same time since PHP 7.x there is no too much time difference in performances.同时,从 PHP 7.x 开始,性能上没有太大的时间差异。 The only way to know really is to do a benchmark based on your own use cases.真正了解的唯一方法是根据您自己的用例进行基准测试。 If there is no special requirement, the normal PHP array is good enough.如果没有特殊要求,普通的PHP array就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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