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 🙂
November 11th, 2008 at 4:33 pm
I got the same time for both loops with PHP 5.2.
Possibly your results were OS-related, with the dumb looping process being given less priority mid-way through running. Did you try it the other way around?