Etienne Kneuss

home » news » SplFastArray to speed up your PHP arrays

SPL Datastructures

The Standard PHP Library was recently completed by a couple of data structures, namely heaps and doubly linked lists. You can find more information about those on
php.net/spl.datastructures

Feeds available

You can read colder.ch directly from the rss/atom feeds:
rss general RSS (general)
rss php RSS (PHP)
atom general Atom (general)
atom php Atom (PHP)

New design : Clearblue

I've made a new skin using the same design with blue colors: Clearblue. Check it out

New website

As you can see, the website has changed completely. I've re-designed everything using different types of technology to give an overall improvement. Take a look in the news section for detailed information.

SplFastArray to speed up your PHP arrays The 7th of June 2008 @ 04:01

Antony got the idea to implement a C-like array wrapper in SPL: SplFastArray. The main advantage of that class is performance, it's indeed faster than PHP arrays. How so? No free lunch: The speedup comes from the fact that non-numeric indexes are not allowed and that the array is of fixed size (which means no hashing and continuous memory storage). That said, here is a quick example:

<?php $a = new SplFastArray(4); // takes the initial size as first argument $a[0] = "foo"; $a[2] = "gee"; $a[3] = "plop"; $a->setSize(3); $a->setSize(5); // increase the size foreach($a as $k=>$v) {     var_dump($v); } /* Output:  * string(3) "foo"  * NULL  * string(3) "gee"  * NULL  * NULL  */ ?>

The speedup seems to vary between different environments, but so far the multiple benchmarks showed that SplFastArray was 10~30% faster than standard PHP arrays.

Comments

07.06.2008 #1 mike.lively

Great idea. I am glad to see this was committed to 5.3. Thanks :)

07.06.2008 #2 brianlmoon

This is super cool. I am always looking for speed ups.

07.06.2008 #3 Trii

I/O is still the major bottleneck in most php apps. A faster array still doesn't make up for poor database performance.

07.06.2008 #4 Amega

Some months ago I had the similar idea of adding new "strict" arrays to PHP, but I'm too lazy to implement =(

With such array, we win not only in speed, but in memory usage.

Hope to see "new arrays" in later versions of PHP.

07.06.2008 #5 ac

aahh, python!

07.06.2008 #6 Antony Dovgal

Good job, Etienne! And thanks a lot for taking it over.

07.06.2008 #7 23JUL

This is simply great!
Can these be used as array keys?
(see: sequence tuple python)

07.06.2008 #8 koreclaps

> Can these be used as array keys?
I suspect not, since they are objects, and array keys must be strings or ints. You'll have to use another data structure. :)

08.06.2008 #9 Jordi Boggiano

So don't you want to link these to the short array syntax ( ["foo, , "gee", "pop"] = your 4 first lines) ?

.. Okay just kidding sorry. Great addition.

08.06.2008 #10 ivan

Am I missing something or SplFastArray is slower than array.
Today I've downloaded php 5.3 and compile it and after this I've made a benchmark for SplFastArray
Here are results:

$ php -q fastarray.php
-------------------------------------------------------------------
marker time index ex time perct
-------------------------------------------------------------------
Start 1212945373.13000300 - 0.00%
-------------------------------------------------------------------
simple array foreach 1212945373.15521900 0.025216102600098 13.68%
-------------------------------------------------------------------
fast array foreach 1212945373.18700200 0.031782865524292 17.25%
-------------------------------------------------------------------
simple array for 1212945373.24330500 0.056303024291992 30.55%
-------------------------------------------------------------------
fast array for 1212945373.31421200 0.070907115936279 38.48%
-------------------------------------------------------------------
Stop 1212945373.31428000 6.7949295043945E-5 0.04%
-------------------------------------------------------------------
total - 0.18427705764771100.00%
-------------------------------------------------------------------

08.06.2008 #11 colder

Could you please provide a link to the source of your benchmarking script ? The one I used had a really big array in order to reduce border effects.

09.06.2008 #12 ivan

No problem. I'll post it latter today when I'm at home

09.06.2008 #13 andrewm

Not to be a stickler, but with the advent of namespaces in 5.3, one might go a little crazy and think that new classes (such as these) could perhaps be namespaced rather than having 'Spl' tacked on the front. :-p Just thinking out loud here.

10.06.2008 #14 ivan

here is code I test with
http://dev.iordanov.net/archives/5

11.06.2008 #15 colder

SplFastArray optimizes the time to add, remove, and generally access elements. You don't benchmark that at all in your file. And yes, iterating/counting on an object is generally slower than iterating/counting on an array, as it represents more work internally.

11.06.2008 #16 ivan

mmm I've missed that.
Anyway .. so it's better to use standard array when I have to iterate rows but if I need to store data in arrays It's better to use SplFastArray
Thanks.

07.08.2008 #17 llama

"multiple benchmarks showed that SplFastArray was 10~30% faster than standard PHP arrays" any chance you could post your benchmarking scripts?

15.06.2009 #18 Phil

You can find details of benchmarking here.

http://www.idontplaydarts.com/2009/06/benchmarking-splfixedarray-access-speed/

Its more like 40% faster for writes, but only about 1% faster for reads. 50% more memory efficient tho.

22.11.2009 #19 Simon

I'm wondering how mature is the SplFixedArray implementation in PHP 5.3. I'm finding it is leading to Segmentation fault (11).

Also, I think it needs to be made clear in the documentation, and examples whether the values of SplFixedArrays can be anything or just restricted in some way. My code uses arrays in the values.

02.02.2010 #20 Simon (again)

I isolated the bug to a few lines of code, and it was accepted as a bug in Dec 2009:
http://bugs.php.net/bug.php?id=50481

$count = 9993;

$cache = array();

for($i = 0; $i < $count ; $i++) {

$cache[$i] = new SplFixedArray(1);

}

Running that causes the segmentation fault.

Add a comment

Username:

Spam Challenge: 13+15=?

Comment: