简体   繁体   English

SplFixedArray :: fromArray - 作为本机固定数组的内存效率如何? PHP 5.3.5

[英]SplFixedArray::fromArray - memory efficient as native fixed Array? PHP 5.3.5

I'm experimenting with SplFixedArray. 我正在尝试使用SplFixedArray。 I have some work done with dynamic arrays which i'm trying to convert to more memory efficient fixed arrays (limited RAM to work with). 我有一些动态数组的工作,我正在尝试转换为更高效的内存固定数组(有限的RAM工作)。

Reading some PHP documentation, found the function in the Title and proceeded to just apply to an array which is like: 阅读一些PHP文档,在Title中找到该函数,然后继续应用于一个数组,如:

$array[x][y]['field']

(3d array with string as index, impossible in fixed arrays) by doing (带有字符串作为索引的3d数组,在固定数组中是不可能的)

$testArray =  SplFixedArray::fromArray(generateArray(256)); 
// generateArray is a function to create the array and set it to zero.

I checked if i could get some memory savings from this versus standard array and no. 我检查了我是否可以通过这个与标准阵列节省一些内存而不是。 Replaced the string index with numbers, same amount of ram used (94 mb) to generate the array. 用数字替换字符串索引,使用相同数量的ram(94 mb)来生成数组。

If i use SplFixedArray properly (not converting from an existing array) i low the mem used to 74mb, but i have a lot of functions and rutines which works with the base 3d array and would be a pain in the ass to convert everything to "proper" fixed array. 如果我正确使用SplFixedArray(不是从现有数组转换)我将使用的内存降低到74mb,但是我有很多函数和rutines可以使用基本的3d数组,并且很难将所有内容转换为“适当的“固定阵列。 That's why when i read abut SPL::fromArray i jumped on my chair. 这就是为什么当我读到SPL :: fromArray时,我跳到我的椅子上。 But with these tests, i found ZERO memory nor speed benefits. 但通过这些测试,我发现ZERO内存也没有速度优势。

Am i not using it correctly? 我没有正确使用它吗? Is this function just for other type of things? 这个功能只适用于其他类型的东西吗?

Thanks! 谢谢!

The short of it is that PHP is not designed to be working with such large data structures in a memory efficient manner. 缺点是PHP不是以内存有效的方式处理这种大型数据结构。 Nothing you do is going to change that. 你所做的一切都不会改变这一点。 Trying to work PHP within a 256MB VPS is very difficult, especially if you've got a web server and database server. 尝试在256MB VPS内运行PHP非常困难,特别是如果你有一个Web服务器和数据库服务器。

As I illustrated in your other question , SplFixedArrays use less memory. 正如我在你的另一个问题中说明的那样,SplFixedArrays使用更少的内存。 That's a fact, and you can look up in the PHP source to see how the objects are created. 这是事实,您可以在PHP源代码中查看如何创建对象。 The numbers don't lie. 这些数字不是谎言。

But it's only one piece of the puzzle... If you are storing lots of big things in the array or are working with other data structures, it's possible that the array isn't the "bottleneck" of memory usage. 但它只是一个难题......如果你在数组中存储大量的东西或正在使用其他数据结构,那么阵列可能不是内存使用的“瓶颈”。

Regarding SplFixedArray::fromArray() , you will definitely be adding to your peak usage, because you are now creating two array structures. 关于SplFixedArray::fromArray() ,你肯定会增加你的峰值用法,因为你现在正在创建两个数组结构。 If you delete the temporary array, you will then be using less memory... but in the interim, you'll be using more. 如果你删除临时数组,那么你将使用更少的内存...但在此期间,你将使用更多。

You'd probably use less peak memory if you just wrote your own function that shifted off an element of the temporary array one by one and added it to the SplFixedArray, as you wouldn't be duplicating your data structure size. 如果您只是编写自己的函数来逐个移出临时数组的元素并将其添加到SplFixedArray中,那么您可能会使用较少的峰值内存,因为您不会复制数据结构大小。 (Due to copy-on-write, the actual savings may not be that big.) (由于写时复制,实际节省可能不会那么大。)

Again, some benchmarks of 1024*1024 sized array with 64-bit integers in each slot: 同样,1024 * 1024大小的数组的基准测试,每个插槽中有64位整数:

SplFixedArray:            92,914,280
array:                   218,756,976
SplFixedArray::fromArray 227,147,408 peak, 92,915,088 after

So as you see, if you load fromArray, you are using more memory, but after the temporary array is deleted, it's back to the savings. 所以如你所见,如果你从theArray加载,你使用的是更多的内存,但是在删除临时数组后,它又回到了节省。 But since the goal is to minimize peak memory usage, using fromArray will be worse than simply using arrays. 但由于目标是最大限度地减少峰值内存使用量,因此使用fromArray会比仅使用数组更糟糕。

My experience is that most of the Spl classes are not real improvements over the native array performance wise. 我的经验是,大多数Spl类并不是对本机阵列性能的真正改进。 At best the tests prove inconclusive, at worse, they show the Spl libraries to be slower. 最好的测试证明是不确定的,更糟糕​​的是,它们显示Spl库更慢。

The major benefits? 主要好处? They are more reliable and consistent objects than array. 它们比数组更可靠,更一致。 SplFixedArray gives you the benefit of knowing that the indexes are ints (numeric indexes, I believe, actually are faster than string indexes), and that you can specifically set the length -- a major bonus if you have no idea how you ended up with some bizarro extra value. SplFixedArray给你的好处是知道索引是整数(数字索引,我相信,实际上比字符串索引更快),并且你可以专门设置长度 - 如果你不知道你最终如何得到一个主要的奖励一些奇怪的额外价值。

I ran some tests on it as well. 我也对它进行了一些测试。 There seems to be no point in using SplFixedArray::fromArray . 使用SplFixedArray::fromArray似乎毫无意义。 The memory savings is hardly noticeable. 节省的内存几乎不可察觉。

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

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