-
god333
-
ocyrus
-
ACR
-
ACR
-
rpag
Since I stumbled upon PHP5’s SPL (Standard PHP Library) I’ve been a fan and supporter of its features, mainly ArrayObject, ArrayIterator and DirectoryIterator. I knew they were not as efficient as the standard “old school” way, but I thought that the standardized interface would outweigh the small (or so I thought) performance hit they would take. Then one day at an interview I had they questioned my use of these and in turn, made myself really question the use of them. So I went home and did some micro benchmarking to prove to myself and anybody else that it was not that much slower.
------------------------------------------------------------------------------------
Array()'s array_push() took: 0.00547194480896 & 101.67704280156Kb of memory
ArrayObject's append() took: 0.0070548057556152 & 101.37743190661Kb of memory
unset() on Array() took: 0.0012061595916748 & -35.175097276265Kb of memory
unset() on ArrayObject took: 0.0039088726043701 & -35.369649805447Kb of memory
ArrayObject's offsetUnset() took: 0.010082960128784 & -97.315175097276Kb of memory
Array() set key => value pair took: 0.0011041164398193 & 39.466926070039Kb of memory
ArrayObject offsetSet() took: 0.0063600540161133 & 101.27626459144Kb of memory
Get Array() value by key took: 0.0010941028594971 & 0Kb of memory
ArrayObject offsetGet() took: 0.006281852722168 & 0.038910505836576Kb of memory
Get ArrayObject value by key took: 0.0011100769042969 & 0Kb of memory
count() on Array() took: 0.0052700042724609 & 0Kb of memory
count() on ArrayObject took: 0.0076661109924316 & 0Kb of memory
ArrayObject's count() took: 0.0050699710845947 & 0Kb of memory
isset() on Array() took: 0.0010261535644531 & 0Kb of memory
iset() on ArrayObject took: 0.0010428428649902 & 0Kb of memory
ArrayObject's offsetExists() took: 0.0059940814971924 & 0Kb of memory
------------------------------------------------------------------------------------
foreach() on Array() took: 0.0032689571380615 & 0.64591439688716Kb of memory
for() on Array() took: 0.0036020278930664 & 0.035019455252918Kb of memory
while() on Array() took: 0.0034470558166504 & 0Kb of memory
foreach() on ArrayIterator took: 0.014889001846313 & 0.20233463035019Kb of memory
for() on ArrayIterator took: 0.033194065093994 & 0.062256809338521Kb of memory
while() on ArrayIterator took: 0.033375024795532 & 0Kb of memory
------------------------------------------------------------------------------------
cURL took: 0.061774969100952 & 0.80155642023346Kb of memory
fsockopen() took: 0.05351185798645 & 0.31906614785992Kb of memory
fopen() took: 0.052693128585815 & 1.3346303501946Kb of memory
stream_socket_client() took: 0.052592992782593 & 0.15953307392996Kb of memory
------------------------------------------------------------------------------------
The results are the mean of 10 tests. As you can see from the results, the SPL functions ARE quite slow, especially the Iterators. The Iterator tests were origionally done with 100,000 array entries, then I knocked it down to 10,000 and finally 1,000, simple because it was taking 10 seconds to iterate over 10,000 array nodes and much longer for the 100,000 array node tests. The performance hit seemed to take a dive in exponentially, which makes sense.
So while there doesn’t seem to be a significant disadvantage when dealing with less data, if you are designing an application that may have to iterate over large arrays I would suggest staying away from SPL until it gets faster, if it ever will.
I’ll have more benchmarks in the future!