SplFixedArray :: fromArray - is memory efficient as its own fixed array? PHP 5.3.5

I am experimenting with SplFixedArray. I have work with dynamic arrays, which I am trying to convert to more efficient fixed-memory arrays (limited RAM for work).

Counting some PHP documentation, I found the function in the header and applied it only to an array that looks like this:

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

(A 3d array with a string as an index, impossible in fixed arrays) by doing

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

I checked if I can get memory savings from this standard array and not. Replaced the row index with numbers, the same amount of used bar (94 mb) to generate an array.

If I use SplFixedArray correctly (without converting from an existing array), I lose the memory used for 74mb, but I have many functions and routines that work with a basic 3d array and will be sick in the ass to convert everything to the "correct" fixed an array. That is why when I read abut SPL :: fromArray, I hopped in my chair. But with these tests, I found ZERO memory and speed advantages.

I do not use it correctly? Is this feature only for other types of things?

Thanks!

+4
source share
3 answers

In short, PHP is not designed to work with such large data structures efficiently. None of this will change. Trying to work with PHP in a 256 MB VPS is very difficult, especially if you have a web server and a database server.

As I illustrated in your other question , SplFixedArrays uses less memory. This is a fact, and you can search the PHP source to find out how objects are created. Figures do not lie.

But this is just one piece of the puzzle ... If you store a lot of big things in an array or work with other data structures, it is possible that the array is not a memory bottleneck.

As for SplFixedArray::fromArray() , you are sure to add to your maximum use, because now you are creating two array structures. If you delete the temporary array, you will use less memory ... but in the interim you will use more.

You would probably use less peak memory if you just wrote your own function that moved the element of the temporary array one by one and added it to the SplFixedArray, since you would not duplicate the size of your data structure. (Actual savings may not be that big due to copy to write.)

Again, some tests are 1024 * 1024 in size with 64-bit integers in each slot:

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

So, as you can see, if you load fromArray, you use more memory, but after the temporary array is deleted, it will return to saving. But since the goal is to minimize peak memory usage, using fromArray will be worse than just using arrays.

+8
source

My experience is that most of the Spl classes are not real improvements over the characteristics of their own array. In the best case, tests are inconclusive, in the worst case they show that library libraries are slower.

Main advantages? They are more reliable and consistent objects than an array. SplFixedArray gives you the idea that indexes are ints (numeric indexes, I believe are actually faster than string indexes), and that you can specifically set the length is a major bonus if you have no idea how you ended up with a bit of added value .

+3
source

I also checked some tests. It doesn't seem to make sense to use SplFixedArray::fromArray . The memory savings are practically not noticeable.

0
source

All Articles