With regards to the new article posted at <
http://code.google.com/
speed/articles/optimizing-php.html>, all of the advice in it is
completely incorrect. We at the PHP team would like to offer some
thoughts aimed at debunking these claims, which the author has clearly
not verified.
1) "Don't copy variables for no reason".
The Zend Engine at the core of PHP 4 and 5 uses a technique known as
"copy-on-write" memory management. This means that no matter how many
times you assign the value of a variable to another variable, the data
is not copied until you change it. The example the author gives
results in absolutely no significant use of extra memory, as shown in
the following example:
<?php
$data = str_repeat("*", 512 * 1024); // synthesize 512K of data
$memory_used_before = memory_get_usage();
$more_data = $data;
$memory_used_after = memory_get_usage();
print "Before: {$memory_used_before}\nAfter: {$memory_used_after}\n";
?>
PHP 5.3 with thread-safety and debugging compiled in:
Before: 853968
After: 854236
PHP 5.2 without thread-safety or debugging:
Before: 581912
After: 581976
A difference of 268 bytes in debugging mode, or 64 bytes in normal
mode (which most people use). Hardly the MB of memory the article
talks about.
It should also be noted that a PHP script should NEVER echo or store
the raw contents of any user-supplied variable without filtering it
appropriately.
2) "Use single-quotes for strings."
Benchmarks run against PHP 5.2 and 5.3 show that parsing double-quoted
strings with interpolation is no slower (and often faster) than single-
quoted strings using concatenation. When simple strings with no
variables in them are used, the performance is clearly better with
double-quoted strings due to implementation details in the engine. See
the benchmark posted at <
http://pastie.org/523023>.
3) "Use echo to print."
Depending on the way PHP is set up on your host, echo can be slower
than print in some cases. Given the example shown, they have equal
performance.
4) "Don't use concatenation with echo."
This is exactly the opposite of correct advice. The engine handles
multiple arguments to echo() in such a way that concatenation (or
double-quoted string interpolation) is actually much faster. See the
benchmark posted at <
http://pastie.org/523020>.
5) "Use switch/case instead of if/else."
Finally, this piece of advice is total nonsense. The decision about
when to use switch/case or if/else is purely a matter of coding style;
their speeds are generally more or less equal except in certain
special cases.
Most of these points may have been true in ancient versions (PHP 3, or
very early versions of PHP 4), but they are definitely untrue in
modern PHP. The PHP team urges the author of the article to check his
facts more carefully, and to investigate where his claims of extra
speed are truly coming from (as certain coding patterns, combined with
specific PHP settings, can sometimes make some of the above points
partially true).
We also urge the author to consider the troubling security
implications of his examples, at least one of which suggests an
extremely dangerous coding style.
In the words of Alexander Songe, if you're really concerned about the
speed of your PHP scripts, look into APC (Alternative PHP Cache,
<
http://www.php.net/manual/en/book.apc.php>) , "which benchmarks show
increases speed 3-5 times over no opcode optimization".
Please feel free to e-mail me at <
gwy...@php.net> (since Google Groups
seems to only want to allow me to use my Gmail address to be in the
group) with questions. You can also join the php-general mailing list
at <
http://www.php.net/mailing-lists.php>, or speak to us on the EFNet
IRC channel #php.