measuring difference between single and double quotes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

measuring difference between single and double quotes

Post 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";
?>
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: measuring difference between single and double quotes

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: measuring difference between single and double quotes

Post 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..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I'd recommend using single quotes on non-interpolated strings just on virtue that you can't accidently interpolate something.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;))
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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." ;)
(#10850)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
Post Reply