In PHP, arrays are one of the most fundamental data structures.
We use them everywhere.
They are very flexible, because they are implemented as associative arrays, and therefore let us use both string and integer keys. They are also unlimited in size, in most languages arrays are fixed-sized, but this is not the case in PHP.
With that in mind, there still is a drawback. It damages performance.
The solution for this problem may be SplFixedArray.
But, it is not a perfect solution.
The two main differences between PHP’s native array and SplFixedArray are that SplFixedArray is fixed-sized (even though the size can be changed at any time using the setSize method) and it uses only integer keys.
In some cases SplFixedArray is a great solutions, it provides a much better way to store data. It also assures us that the indexes will be numbered from 0 to the size of the array minus one.
I won’t discuss the advantages and disadvantages of both sides, as this issue was discussed many times earlier, instead I will discuss the difference in performance.
I have made 3 tests to check the performance:
- Writing data to the array. (Run time and memory for each data structure)
- Reading data from the array. (Run time for each data structure)
- Getting random value from the array. (Run time for each data structure)
The results were not very surprising, but they were somehow disappointing.
I ran the testes on my PC and got the results, in this article I will only publish the results and conclusions, you can download the test files and the excel files used to analyze the results.
Writing data to the SplFixedArray took about 75% of the time it took writing to the native array at any case.
The memory consuming were better news – SplFixedArray consumed 42% of what the native array consumed.
Both of the arrays scaled linearly with the enlargement of the arrays.
There was almost no difference in the time it took to iterate over a native array and a SplFixedArray.
Both were linear.
Getting random value
In this test I used two different ways to read random data from the arrays.
The first way is the function array_random, which apparently does not work on SplFixedArray.
The second way is generating a random number and getting the value with that key.
I should mention that the second method can always work on SplFixedArray (as the indexes are always continuous from 0), but not always on the native array (as the indexes may not be continuous or even string only).
In this test I took 5 random elements from the array every time.
The results were surprising!
The second way was at about the same for 1, 10, 100, 1000, 10,000 and 100,000 elements in the array, but the first way was linear with the scale of the array!
This is an extremely slow performance, and one more bad point for native arrays.
Actually it is hard to come into a conclusion.
Generally speaking SplFixedArray gives better performance, less memory and less run time – both by constant multiplier.
When you can, such as when you for some reason want to save rows from a database to an array, try and use SplFixedArray, the difference in performance won’t be huge but they are there.