Posts Tagged ‘php’

* PHP: long variable names

Posted on July 29th, 2008 by whinger. Filed under Web Development.


edit: as Chris points out in the comments, this doesn’t hold true for php 5.2. Yah, boo. 🙁

So using long variable names in php makes a difference to the speed of your code.

It’s true: try this…

<?
$s=time();
for ($c=0; $c < 100000000; $c++) {
$verylongvariablename++;
}
print time()-$s . "\n";

$s=time();
for ($c=0; $c < 100000000; $c++) {
$a++;
}
print time()-$s . "\n";

?>

This gives me a difference of around 20%.

Of course in the real world you won’t just be incrementing variables in a tight loop so the difference is essentially irrelevant and I wouldn’t recommend anyone starts sacrificing readability by shortening variable names, but I thought it was interesting.

Who knows, you might too 🙂

Tags: .



Blogroll

Categories