Making something in PHP isn’t hard. Things get harder when it needs to be secure and when speed becomes relevant. But what to do when things really have to be at there absolute fastest? What if every µsecond counts? What if a single extra CPU cycle needed to run your script can cost a man his life? Read on.
Ok, so an extra CPU cycle needed to run a PHP script won’t save lives.. I thought it sounded nice.
I oftenly see people use functions in places where, sure, they work, but others would do the exact same, much faster. And to be honest, I can’t blame those people. Its not like there is a guide on it (if there is / you have one, leave a comment with the URL, please. Thanks.) and figuring out what function can do something the fastest requires you to know all functions that can do what you want, plus doing some decent testing on all of them. The latter is a big problem, ’cause as we all know, us programmers are lazy. 😉 Anyway, for those that really are, I’m going to post some of the benchmarks I ran myself on stuff.
How I benchmark
First a short elaboration on how I run my benchmarks.
I benchmark things at a verry basic level. Setup some stuff, start a timer, do something xx times (normally 100,000 or 1,000,000, though it may be up or down a zero for verry light or heavy things.), run what I’m comparing it against the same ammoung of times, print out how long both took. Like I said, verry basic. 🙂 Note to self: Finish that class for automating this, soon..
For those who care: I run stuff on a Intel Pentium 4, 3066 MHz (23 x 133) (Northwood HyperThreading), 2 X Corsair VS1GB400C3, plugged into a MSI MS-6575E motherboard. My OS of use is Windows XP Professional running Apache/2.0.59 & PHP/5.2.0. (That about sums it up, right?)
isset() vs array_key_exists()
Note that there is a difference between the two. isset($array[$index]) returns false even if it is actually set to NULL, whereas array_key_exists() returns true. Thank you Jeremy for pointing this out.For a project I’m working on with Pete, I’m oftenly checking if an index is set in a array. I normally use isset to do this, but sometimes, for no reason, use array_key_exists() instead. When I looked over some code I has written, I noticed this, and decided to see which is better. I used an array containing incremently more letters of the alphabet, untill an element held all. (So 0 => ‘a’, 1 => ‘ab’,…24 => ‘abc…xyz’) I then had both isset() and array_key_exists() check elements 0 trough 26 (of which all but 26 would be set) 1,000,000 times. (So, 27,000,000 calls to both functions.) There are the results:
In total, this benchmark took 16.87498 seconds.
Times where spend like this:
isset() took 1.5900671482 seconds.
array_key_exists() took 15.2848100662 seconds.
This suprised me, as I would have assumed array_key_exists would be faster. I then wondered, would there be a difference if the keys would be alphanumerical, instead of just numerical?
So, first, I made another benchmark. It was exactly the same, except for the array used; it now was array(‘a’ => ‘a’,…’abc..xyz’=>’abc…xyz’); I did still have both functions check if a numerical value was set. While not 100% unexpectable, this didn’t seem to make a big difference. I then had the functions call the alphanumerical indexes (‘a’ => ‘abc..xyz’) and as 27th check, I used ‘zyx..cba’. My results:
In total, this benchmark took 23.82291 seconds.
Times where spend like this:
isset() took 4.5246219635 seconds.
array_key_exists() took 19.2981789112 seconds.
While still not exactly what I expected (a function written to check if a key is set in an array should be faster then a function written to check if a variable is set and that happens to be able to do the same for array indexes, right?) I do find the relative increases interesting. Even though isset() is still much faster, it seems array_key_exists() is has much less problems with strings. (Note: I tested with different lengh loops, and found that the fact that the used time went up about 4 seconds for each is a coincidense.) I won’t write a conclusion, because I
suck at writing those don’t want to make my own thoughts seem like facts, but I know I’ll be sticking with isset() from now on. Isset(), faster in executing and writing. 😀 Oh, if your interested in seeing what exactly I did, and how it runs on your system (users of other platforms, both hard- as softwarematic, I’d like to see your results.), just for you, I uploaded a package containing the file here.
for now, at least. Yea, I suck at writing out more then one thing at a time. I’m going to post follow-up articles lateron. I hope somebody gets something usefull out of this. 😛