performance / tuning tips. to the point.
|
Faster PHP Code by Examples
PHP's powerful position as a Web programming language needs no justification. Large Web sites today are running PHP,
and they performance extremely well: think digg.com and facebook.com. This article will summarized some best practices
in coding in PHP on the LAMP environment.
The article compares two examples (before and after) which showed the drastic improvements in
response time:
PHP's for loops are just like C's for loops. You can place arbitrary expressions
in the initializer, test, and increment; do not put a call to a function in test:
bad for performance (called N times):
for ($i=0; $i < $b=complicated_function(); $i++)
good for performance (called N times):
for ($i=0,$b=complicated_function(); $i < $b; $i++)
|
There are two regular expression libraries that shipt with PHP: Posix and Perl-style.
Posix regular expressions are simpler, but often slower.
Perl-style regular expressions tend to be faster and are a lot more powerful -- but some find
them complex. But stick with the Perl-style regular expressions since these are the standard
in many other languages.
|
You often need to have arguments to your database queries, e.g.:
SELECT title, pub_date, price FROM book WHERE book_id=3
this is fine functionally, but performance-wise, it is poor and can be dangerous:
- You must escape the arguments correctly (e.g., handle quotes)
- You prevent the database from performance some caching optimization on query compilation
- If the argument comes from the user, you may be leaving yourself open to a number of security
vulnerabilities (SQL injection).
Bind the arguments to resolve the issue:
$query= $conn->query("SELECT title, pub_date, price FROM book WHERE book_id=?", Array(2));
if (DB::iserror($query)) {die ($query->getMessage());}
|
Parsing XML documents:
DOM
- easier to implement when re-noding a tree is required often
- memory hog when doc size is large
SAX
- faster to process than DOM
|
|
Couldn't find what you need? Check out our blog entries.
|
|