PHP 5.3 extended the Standard PHP Library, it came with some new-fancy data structures such as SplStack, SplMaxHeap and many more.
In this article I would like to focus one of them – SplFixedArray.

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.

Tests

I have made 3 tests to check the performance:

  1. Writing data to the array. (Run time and memory for each data structure)
  2. Reading data from the array. (Run time for each data structure)
  3. 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.

Results

Writing data


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.

Reading data

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.

Conclusions

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.

Tagged with:
 

9 Responses to PHP’s native array vs SplFixedArray performance

  1. Alon Gubkin says:

    The new SPL data structures are a huge step in improving the PHP performance, which is a very weak side of PHP.

    Great post, keep it up :)

    • Thanks for the comment!
      Yeah, Spl really is great, it is not only a boost for performance, but it also gives us a better way to write applications and debug them.

  2. John says:

    Great article, but one thing you also have to account for is the creation time of an object vs an array (which I mentioned in my article http://www.johnciacia.com/2011/02/01/array-vs-splfixedarray/) – I concluded that if you are creating a lot of small SPLFixedArrays, it is probably better to use the primitive array as the overhead of object creation exceeds the poor write time of the native array.

  3. Cory says:

    You seems to be an expert in this field, great articles and keep up the great work, my buddy recommended me it.

    My blog:
    Regroupement De Credit ou rachat de credit immobilier

  4. google says:

    I liked your article is an interesting technology
    thanks to google I found you

  5. [...] Shay Ben Moshe gibt für PHP 5.3 und große Arrays eine kurze Übersicht zur Performance von numerischen SplFixedArrays. [...]

  6. wandad says:

    I want to thank you for your article.
    Its so useful for PHP freak like me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.