Welcome Guest, Not a member yet? Register   Sign In
40 Tips for PHP Optimization
#1

[eluser]Michael Wales[/eluser]
40 Tips for PHP Optimization

Some of my favorites (many of these are mentioned in the EllisLab Developer Guidelines as well):
Quote:9. See if you can use strncasecmp, strpbrk and stripos instead of regex

10. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4

15. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.

24. Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside '...'. Of course you can only do this when you don't need to have variables in the string.

29. Use ip2long() and long2ip() to store IP addresses as integers instead of strings in a database. This will reduce the storage space by almost a factor of four (15 bytes for char(15) vs. 4 bytes for the integer), make it easier to calculate whether a certain address falls within a range, and speed-up searches and sorts (sometimes by quite a bit).

30. Partially validate email addresses by checking that the domain name exists with checkdnsrr(). This built-in function checks to ensure that a specified domain name resolves to an IP address. A simple user-defined function that builds on checkdnsrr() to partially valid email addresses can be found in the user comments section in the PHP docs. This is handy for catching those occasional folks who think their email address is ‘[email protected]’ instead of ‘joeuser@Php.net’.

32. Learn to love the ternary operator.

40. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
#2

[eluser]leonglass[/eluser]
I am going to bookmark that for later. I think his point in 40 does apply to other languages as well as they also convert to less opcodes for prefix incrementing. It is something I have done as standard for a long time in Java.
#3

[eluser]Unknown[/eluser]
[quote author="walesmd" date="1192482883"]40. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
[/quote]
This's false for other languages as it's not php specific. In fact, you really get an optimization with ++i instead of i++ in C and C++ code, because the first to a simple add (and often an atomic one), but the second makes an assignment and an add. Things can be of course optimized the compilers, but with ++i you're sure to have the fatest code in all cases.




Theme © iAndrew 2016 - Forum software by © MyBB