google's performance tips and nonsense micro optimizations
Jun 25, 2009
google has published bunch of articles named "http://code.google.com/speed/articles/". i couldn't have the chance to look at all the articles but the tips mentioned in the php performance tips article are quite incorrect and nonsense.here are the nonsense, or incorrect tips and their corrections.
Don't copy variables for no reason.
Sometimes PHP novices attempt to make their code "cleaner" by copying predefined variables to variables with shorter names. What this actually results in is doubled memory consumption, and therefore, slow scripts. In the following example, imagine if a malicious user had inserted 512KB worth of characters into a textarea field. This would result in 1MB of memory being used!BAD:
$description = $_POST['description']; echo $description;
GOOD:
echo $_POST['description'];
• the truth is php has "copy-on-write" memory management which means data is not copied to a variable unless it's changed.
Use single-quotes for strings.
The PHP engine allows both single-quotes and double-quotes for string variable encapsulation, but there are differences! Using double-quotes for strings tells the PHP engine to read the string contents and look for variables, and to replace them with their values. On regular strings which contain no variables, that can result in poor performance. It's better to use concatenation than double-quoted strings.BAD:
$output = "This is a plain string";GOOD:
$output = 'This is a plain string';BAD:
$type = "mixed"; $output = "This is a $type string";
GOOD:
$type = 'mixed'; $output = 'This is a ' . $type .' string';
• there's no significant difference you can notice using either double quotes or single quotes. sometimes double quotes are faster than single quotes. my php coding preference is same as described in google's last good example though.
Use echo to print.
Using the echo() function to print results in better readability and, as in the next example, better performance.BAD:
<?php print($myVariable); ?>GOOD:
<?php echo $myVariable; ?>
• does it really matter saving 0.00000001 seconds? let's pretend it really does matter for a second. let's say we really want to find out which is faster.
first let's download vulcan logic disassembler. vld dumps the zend engine opcodes of a php script. it can be used to see what's going on inside the engine. after you download vld, install it like you do for any other php extension;
$> phpize $> ./configure $> make install
and enable it in the
php.ini file.(extension=vld.so). then create two simple scripts similar to followings(remember we try to figure out the faster one)
<?php print("hello world");
and the other one for
echo
<?php echo "hello world";
and we can execute the scripts,
php -d vld.active=1 script1.php
function name: (null)
number of ops: 4
compiled vars: none
line # op fetch ext return operands
-------------------------------------------------------------------------------
3 0 PRINT ~0 'hello+world'
1 FREE ~0
2 RETURN 1
3* ZEND_HANDLE_EXCEPTION
and the script with the
echo
function name: (null)
number of ops: 3
compiled vars: none
line # op fetch ext return operands
-------------------------------------------------------------------------------
3 0 ECHO 'hello+world'
1 RETURN 1
2* ZEND_HANDLE_EXCEPTION
as you can see
print uses one more opcode. you can say "echo is faster than print" by looking at this example but one opcode is nothing.Use switch/case instead of if/else.
Using switch/case statements rather than if/else statements for a single variable results in better performance, readability, and maintainability.BAD:
if($_POST['action'] == 'add') {
addUser();
} elseif ($_POST['action'] == 'delete') {
deleteUser();
} elseif ($_POST['action'] == 'edit') {
editUser();
} else {
defaultAction();
}GOOD:
switch($_POST['action']) {
case 'add':
addUser();
break;
case 'delete':
deleteUser();
break;
case 'edit':
editUser();
break;
default:
defaultAction();
break;
}
• this has nothing to do with performance. this is a coding style. well, yes switch/case is said to be faster but i advise you to read some brian kernighan(the practice of programming, page 13).
no offense but google's advices are quite irrelevant. if you code php, you shall know that most of the time(let's be honest, all the time) you need to use some sort of a opcode caching such as apc, eaccelerator, xcache.
don't waste your time on nonse micro optimizations. they don't worth it. add the missing indexes in your databases, don't hit the database 100 times in every single page, denormalize if you have to. once you finished doing these, world will be a better place and you'll find yourself configuring memcached, varnish, ncache, squid-cache and sort of things.