4 ms·
As someone who optimized a piece of PHP code recently, there are a number of things I ran into that were counterintuitive wrt performance. - array_key_exists i
by gfodor 15y ago
As someone who optimized a piece of PHP code recently, there are a number of things I ran into that were counterintuitive wrt performance.
- array_key_exists is 5X slower than isset (though there are different semantics)
- direct string concatenation is 2X faster than using "implode"
- memoization can be a huge win since function calls are 5X slower than array accesses. (No inlining in PHP)
Of course, these optimizations don't matter except in your hotspots. (Why we still have to have this disclaimer on HN puzzles me, but people seem to keep trotting out the usual Knuth quote out of context as a way to write off micro-optimization techniques in general.)
Also important to keep in mind:
- PHP has copy on write semantics for arrays. So if you set $foo = $bar (arrays), you don't incur any additional memory until you alter $foo. (Aside from the additional reference.) Once you change any entry of $foo, PHP makes a copy of the whole thing. (This can result in massive performance and memory bloat if you don't realize its happening.)
- PHP arrays are not arrays, but are a hybrid linear array and hashtable. ("one data structure to rule them all.") So, even a simple "array" of integers incurs more than what you'd expect memory wise. In fact, IIRC, an array of integers incurs approximately 100 bytes of memory for each entry. Ouch. There are extensions in new versions of PHP that allow you to use 'real' arrays. If you're stuck using normal PHP arrays, good luck trying to design optimized data structures for the problem at hand.
See also: http://phpbench.com/ http://phpbench.com/
- ck2 15y agoEven more micro-optimization: use single quotes for strings instead of double so PHP does not try to parse it. (phpbench seems to dispute this but doesn't say which version of PHP they are using?) Don't forget the strange performance hit from require_once vs require, use your own flags instead.
- rimantas 15y agoDon't worry about quotes (PHP takes car of that now) and use autoloading instead.
- Helianthus 15y agoThanks for these. I read the article and thought "These are kinda basic..." whereas I just had to decide between implode and concatenation. edit: in retrospect, what I was trying to say was essentially "Hacker news can write a better article on php optimization, just in the comments alone." Elitism? probably.
- al_james 15y ago>direct string concatenation is 2X faster than using "implode" That depends on how many strings you are concatenating. The beauty of implode is that you can build an HTML snippet and implode it all at the end before output.
- lhnz 15y ago> There are extensions in new versions of PHP that allow you to use 'real' arrays. Where?
- dqminh 15y agoThere is SplFixedArray http://www.php.net/manual/en/class.splfixedarray.php http://www.php.net/manual/en/class.splfixedarray.php, not sure if this is the one he mentioned though.