Saturday, March 17, 2007

PHP Optimization Tricks

There are a number of tricks that you can use to squeeze the last bit of performance from your scripts. These tricks won't make your applications much faster, but can give you that little edge in performance you may be looking for.

1.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.

2.When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. But Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.

Ex.
if (strlen($foo) <> 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }

The bottom search mechanism is roughly 3 times faster.

9.file including and requiring.

First, you should only use require if you KNOW you will need that file on that page. If you MIGHT use it, use
include instead because PHP opens up all files that are required, whether the script gets there or not. To help out the programmer,
I suggest using "include_once" and "require_once".

Second, get your source file sizes as small as possible and only include/require files you absolutley must have.
If you only need one function in a file, consider breaking that function into its own file. I found on my system
that for every KB of source code that was included/required, I lost one transaction per second (tps). This may
not lead to perfectly organized code, but if speed is a concern, it is definitley worth it.

10.true is faster than TRUE
How come true is faster than TRUE?
This is because when looking for constants PHP does a hash lookup for name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups.

11.Perl-regexps are faster than POSIX-regexps
http://www.oreilly.com/catalog/regex/ This books explains regex and why a posix regex is slower.

12.FOR vs. WHILE vs. DO-WHILE
just out of curiosity I wanted to see which one of these structures are faster. I believed that "for" will be the fastest but the tests proved I was wrong.

for: 2.078712940216064
while: 1.981828927993774
do: 1.936743974685669

For 2,000,000 iterations the do-while structure is almost 0.15 seconds faster than the for structure. This won't mean very much for small sites but in complex application where you'll have to handle many hits it will make a difference.

13.Here are two tips for loops. If the number of iterations in a loop is low, you might get some performance gain from replacing the loop with a number of statements. For example, consider a for loop that sets 10 values in an array. You can replace the loop with 10 statements, which is a duplication of code, but may execute slightly faster.

Also, don't recompute values inside a loop ex:- count(arrname) .The price for coding in this style was a slight hit in performance, as the loop calls the count function repeatedly.

If you know or have any additional optimization tricks let me know.

References:-
http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
//Zend

No comments: