Page 1 of 4

measuring difference between single and double quotes

Posted: Fri Apr 14, 2006 8:56 am
by timvw
In my test with php5.1.2 i experienced that single quotes are faster than double quotes.
In my test with php4.3.10 and php4.4.2 i experienced that double quotes are faster than single quotes.

What are your results? ;)

Here's the code that i've used..

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

function microtime_float() {
        // when you're using php5 uncomment the following line:
        // return microtime(true);
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
}

$times = array();
$times['single'] = 0;
$times['double'] = 0;

for ($i = 0; $i < 100000; ++$i) {

        $start = microtime_float();
        echo "How are you today?<br/>";
        $times['double'] += (microtime_float() - $start);

        $start = microtime_float();
        echo 'How are you today?<br/>';
        $times['single'] += (microtime_float() - $start);
}

echo 'Results for single quotes: ' . $times['single'] . "\n";
echo 'Results for double quotes: ' . $times['double'] . "\n";
?>

Posted: Fri Apr 14, 2006 9:18 am
by Oren
Sounds kinda strange because as far as I know single quotes should be faster since the parser does variable interpolation in double quoted strings but not with a single quoted strings.

Re: measuring difference between single and double quotes

Posted: Fri Apr 14, 2006 9:18 am
by Benjamin
timvw wrote:

Code: Select all

// when you're using php5 uncomment the following line:
        // return microtime(true);
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
I wouldn't uncomment that because it might return a result faster and skew the results.

Posted: Fri Apr 14, 2006 9:29 am
by Benjamin
Here are my results on PHP5 without return microtime uncommented..

Results for single quotes: 1.6528582572937 Results for double quotes: 1.501455783844

It's negligable.

Re: measuring difference between single and double quotes

Posted: Fri Apr 14, 2006 9:32 am
by timvw
agtlewis wrote:
timvw wrote:

Code: Select all

// when you're using php5 uncomment the following line:
        // return microtime(true);
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
I wouldn't uncomment that because it might return a result faster and skew the results.
I'm comparing '' versus "".. Not php4 versus php5... So i don't see how it makes a difference in that case..

Posted: Fri Apr 14, 2006 9:47 am
by Benjamin
Ok check this out... PHP5 1,000,000 loops

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

function microtime_float() {
  return microtime(true);
  //list($usec, $sec) = explode(' ', microtime());
  //return ((float)$usec + (float)$sec);
}

$times = array();
$times['single'] = 0;
$times['double'] = 0;

for ($i = 0; $i < 1000000; ++$i) {
  $start = microtime_float();
  $One   = "10";
  $Two   = "20";
  $Three = "30";
  $Four  = $One + $Two + $Three;
  $times['double'] += (microtime_float() - $start);

  
  $start = microtime_float();
  $One   = '10';
  $Two   = '20';
  $Three = '30';
  $Four  = $One + $Two + $Three;
  $times['single'] += (microtime_float() - $start);
}

echo 'Results for single quotes: ' . $times['single'] . "\n";
echo 'Results for double quotes: ' . $times['double'] . "\n";
?>
Results for single quotes: 10.18602514267 Results for double quotes: 10.240032911301

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

function microtime_float() {
  return microtime(true);
  //list($usec, $sec) = explode(' ', microtime());
  //return ((float)$usec + (float)$sec);
}

$times = array();
$times['single'] = 0;
$times['double'] = 0;

for ($i = 0; $i < 1000000; ++$i) {
  $start = microtime_float();
  $One   = "a";
  $Two   = "b";
  $Three = "b";
  $Four  = $One . $Two . $Three;
  $times['double'] += (microtime_float() - $start);

  
  $start = microtime_float();
  $One   = 'a';
  $Two   = 'b';
  $Three = 'c';
  $Four  = $One . $Two . $Three;
  $times['single'] += (microtime_float() - $start);
}

echo 'Results for single quotes: ' . $times['single'] . "\n";
echo 'Results for double quotes: ' . $times['double'] . "\n";
?>
Results for single quotes: 9.037769317627 Results for double quotes: 9.0633971691132

They are almost the same.

Posted: Fri Apr 14, 2006 9:58 am
by Ambush Commander
I'd recommend using single quotes on non-interpolated strings just on virtue that you can't accidently interpolate something.

Posted: Fri Apr 14, 2006 10:31 am
by timvw
agtlewis wrote:They are almost the same.
That's all i was trying to find out :)


(In a discussion on comp.lang.php someone argued that there was a significant difference, and i wasn't able to reproduce that ;))

Posted: Fri Apr 14, 2006 12:10 pm
by Oren
You can't make conclusions after running this script for few times. You need to run it for at least 1000 times and on different OSs as well.

Personally, even though the difference isn't significant I would use the single quotes as it is a little bit faster. When working with large applications then the small things make the large things - 0.00001sec + 0.00001sec + ... + 0.00001sec for 100000000000 times does matter so don't forget that.

Posted: Fri Apr 14, 2006 1:38 pm
by alex.barylski
By just knowing how double and single quotes work you know that single quotes will always be faster. Unless there is some hidden magic going on in single quotes that I'm not aware of - Thought I should make note of that!


There are so many factors which might skew results slightly in favour of double quotes on one machine and not on another or visa-versa it's almost useless to profile, especially using PHP.

The only way, to truly profile a section of code, is to keep the machine abstraction to an absolute minimum (Which PHP ain't doing) or even profile in assembly (not nessecarily the code you write but profiling code) and execute those set of instructions from a cold boot, so there is no TSing, Paging, Caching and other complicated OS processes occuring simultaneously or at least being emulated.

Just my two cents :)

Posted: Fri Apr 14, 2006 3:47 pm
by BDKR
Oren wrote: Personally, even though the difference isn't significant I would use the single quotes as it is a little bit faster. When working with large applications then the small things make the large things - 0.00001sec + 0.00001sec + ... + 0.00001sec for 100000000000 times does matter so don't forget that.
I couldn't agree more. In the world of very busy services that need to be highly available (in more ways then one) these little things have a cumulative effect.

Posted: Fri Apr 14, 2006 4:15 pm
by alex.barylski
BDKR wrote:
Oren wrote: Personally, even though the difference isn't significant I would use the single quotes as it is a little bit faster. When working with large applications then the small things make the large things - 0.00001sec + 0.00001sec + ... + 0.00001sec for 100000000000 times does matter so don't forget that.
I couldn't agree more. In the world of very busy services that need to be highly available (in more ways then one) these little things have a cumulative effect.
IMHO simple optimizations like this boil down to basic programmer responsibility...

As a professional, you should optimize when it's obvious...it's not like using string concatenation is difficult or much more so compared to interpolation.

Anything less is lazy programming - which were all guilty of to some degree I guess :)

Posted: Fri Apr 14, 2006 4:23 pm
by Christopher
Hockey wrote:...it's not like using string concatenation is difficult or much more so compared to interpolation.
I don't think there was a test for this e.g.

$Four = $One . $Two . $Three;

vs.

$Four = "$One$Two$Three";
Hockey wrote: - which were all guilty of to some degree I guess :)
On a good days it's only " to some degree." ;)

Posted: Fri Apr 14, 2006 10:40 pm
by BDKR
Hockey wrote: As a professional, you should optimize when it's obvious...it's not like using string concatenation is difficult or much more so compared to interpolation.
Well I happen to agree with this. It's funny, but in times past on this board and other places around the net, you would have been (speaking from experience) heckled and labeld a code smith or guilty of such sins as "pre-mature optimization". As it turns out, these mooks are all reading from the same playbook.

Posted: Sat Apr 15, 2006 1:05 pm
by alex.barylski
BDKR wrote:
Hockey wrote: As a professional, you should optimize when it's obvious...it's not like using string concatenation is difficult or much more so compared to interpolation.
Well I happen to agree with this. It's funny, but in times past on this board and other places around the net, you would have been (speaking from experience) heckled and labeld a code smith or guilty of such sins as "pre-mature optimization". As it turns out, these mooks are all reading from the same playbook.
I'm glad we agree...brilliant minds think alike ;)

In anycase, I know what your talking about, but I think what most seasoned professionals are getting at when they heckle, is more geared towards algorithm optimization than trivial optimizations like the one mentioned above. Alot of development time is wasted when you spend all day optimizing one function, when really you can't truly tell if that function needs optimization until you have a fully functioning application.

Although algorithm optimizations will likely have the largest effect on performance, I often just prefer to make it work, leaving optimizations for later or never at all.

Cheers :)