Stop Micro-optimizing

November 10, 2009
By SeanJA

Let’s look at a common php ‘optimization’ tip (note that I am only running this test on my computer, and I am not shutting things down to help it go faster).

Use single quotes ( ‘ ) instead of double quotes ( ” ) .

The reasoning behind this is that php parses double quoted strings for variables, so it takes way longer to process your evil double quoted strings than it does to parse your single quoted strings.

$test = 'something';
$repeats = 1000000;
$start1 = microtime(true);
$first = '';
for($i = 0; $i < $repeats; $i ++){
	$first .= 'string '. $test;
}
$end1 = microtime(true);
$time1 = ($end1 - $start1);
 
$start2 = microtime(true);
$second = "";
for($i = 0; $i < $repeats; $i ++){
	$second .= "string $test";
}
$end2 = microtime(true);
$time2 = ($end2 - $start2);
 
echo 'A difference of: '.($time2 - $time1) . ' seconds'; 
#=> A difference of approx: 0.244801044464 seconds;

A whopping 0.24 seconds over 1000000 string concatenations (a difference of about 0.00000024 per concatenation). I wouldn’t bother with this one, if the code is full of double quotes leave it.

Now, I am not suggesting that you rethink the micro optimizations that you make go ahead, make them if you must. But before you put too much thought into it, make those macro-optimizations that you are avoiding and clean up your code. Future you will thank you, especially if current you avoids dumb “micro-optimizations” like bit shifting rather than multiplying and dividing (dumb because for the most part, modern compilers make this optimization already, or your processor does a table lookup rather than doing the actual calculation).

#this:
9 * 256;
#is way more readable than:
9 << 8;
Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • HackerNews
  • Technorati
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • Print
  • PDF
  • email

Related posts:

  1. Damn-it PHP
  2. On naming functions sanely
  3. Getting Started With PHPUnit
  4. Documenting PHP Code
  5. PHP Functions, you’re doing it wrong…

4 Responses to Stop Micro-optimizing

  1. SeanJA on December 1, 2009 at 7:11 am

    I do too to be honest, which really screws me up when I switch over to java… What do you mean invalid char? The point is, if you are working on a project that already has double quotes, don’t worry about changing them up.

    Side note: I think the single quote looks cleaner (and you have to hit shift less, so it is a coding optimization).

  2. SeanJA on November 13, 2009 at 5:41 am

    If you are serving millions of users, I hope that you have the good sense to implement caching of the generated HTML and the generated bytecode. At that point too, you should be looking at optimizations (a better database schema, better database queries, better algorithms…) and then maybe, if you are still having problems start micro-optimizing.

  3. PhiLho on November 12, 2009 at 4:56 pm

    Well, I would answer: “Stop making meaningless micro-benchmarks”…
    If I am not mistaken, the micro-optimization is about time to convert PHP source code to bytecode. So your brenchmark measures nothing.
    Now, the difference is probably small for lot of applications. But it might do a difference for Web sites with millions of users, or large files mostly holding localization strings.

Leave a Reply

Your email address will not be published. Required fields are marked *

*