November 10th, 2009


10
Nov 09

Stop Micro-optimizing

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;

10
Nov 09

Daily Digest for November 10th

twitter (feed #2)
New blog post: Daily Digest for November 9th http://bit.ly/3fezqj [SeanJA]
googlereader (feed #5)
twitter (feed #2)
"I’d rather endure ants eating my eyeballs than have to listen to Celine Dion mooning about something or other for hours on end" – My Dad [SeanJA]
twitter (feed #2)
In other news… Rupert Murdoch has gone insane http://bit.ly/isRIV [SeanJA]
googlereader (feed #5)
twitter (feed #2)
Got my "Available Online for Free" stickers… What should I put them on? [SeanJA]
twitter (feed #2)
Looks like Robert Plant is playing in someone’s house (it is actually a pub)… http://is.gd/4RfWW (via @hijinksensue) [SeanJA]
googlereader (feed #5)
googlereader (feed #5)
twitter (feed #2)
@gavinblair True story [SeanJA]
twitter (feed #2)
In Toronto, you are not allowed to keep mongooses, pigs or anteaters [SeanJA]
twitter (feed #2)
101 on jQuery Selector Performance Performance, Scalability and Architecture http://bit.ly/2Xt2gd (it uses dynaTrace) [SeanJA]
twitter (feed #2)
@lyndzrokz Don’t ever joke about Star Wars [SeanJA]
twitter (feed #2)
RUN It’s an evolutionist! http://bit.ly/2zXuac [SeanJA]