/    Sign up×
Community /Pin to ProfileBookmark

double quotes vs. single quotes

In response to the discussion in [url=http://www.webdeveloper.com/forum/showthread.php?t=111268]this thread[/url] concerning performance differences between the use of single and double quotes, I decided to do some testing.

Test script:

[code=php]
<?php
header(“Content-Type: text/plain”);
function single_quote()
{
$start = microtime(TRUE);
$string = ‘This is a test. It is only a test. Testing 1, 2, 3. ‘.
‘This has been a test. It was only a test. 3, 2, 1, the end.’;
return(microtime(TRUE) – $start);
}
function double_quote()
{
$start = microtime(TRUE);
$string = “This is a test. It is only a test. Testing 1, 2, 3. “.
“This has been a test. It was only a test. 3, 2, 1, the end.”;
return(microtime(TRUE) – $start);
}
function heredoc_str()
{
$start = microtime(TRUE);
$string = <<<EOD
This is a test. It is only a test. Testing 1, 2, 3.
This has been a test. It was only a test. 3, 2, 1, the end.
EOD;
return(microtime(TRUE) – $start);
}
$seed = microtime(TRUE); // because first call is always slow
$methods = array(‘single_quote’, ‘double_quote’, ‘heredoc_str’);
for($ix=0; $ix<20; $ix++)
{
array_rand($methods);
foreach($methods as $method)
{
$times[$method][] = call_user_func($method);
}
}
$methods = array(‘single_quote’, ‘double_quote’, ‘heredoc_str’);
foreach($methods as $method)
{
$averages[$method] = sprintf(“%01.6f”,
array_sum($times[$method]) / count($times[$method]));
}
echo “Averages (seconds):n”;
print_r($averages);
echo “nRaw Data:n”;
print_r($times); // show raw data
?>
[/code]

Typical Results (run the script yourself if you want to see the raw data section):

[code]
Averages (seconds):
Array
(
[single_quote] => 0.000036
[double_quote] => 0.000035
[heredoc_str] => 0.000076
)
[/code]

This was run on my awesomely un-powerful 500MHz Pentium III with a non-whopping 256MB of RAM, running PHP 5 on Apache. As a result of this, I’m not going to lose any sleep over the difference between single or double quotes, but will only use heredoc when necessary.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@Leo_01Jun 23.2006 — Nice.

i never knew that 0.000001 sec is very importent to double quote guys.
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Do keep in mind that your computer is doing a dozen other things at the same time that you are running your bench mark script. You may be measuring those other things more than you are quotes.
Copy linkTweet thisAlerts:
@NogDogauthorJun 23.2006 — Do keep in mind that your computer is doing a dozen other things at the same time that you are running your bench mark script. You may be measuring those other things more than you are quotes.[/QUOTE]
Which is why I ran multiple iterations with the $methods array being randomly sorted on each iteration. Here are the results of running it with 200 iterations through the test loop:
<i>
</i>Averages:
Array
(
[single_quote] =&gt; 0.000037
[double_quote] =&gt; 0.000037
[heredoc_str] =&gt; 0.000081
)
Copy linkTweet thisAlerts:
@Leo_01Jun 23.2006 — Any what are the chances of a a actual server doing other things too when it is processing your script?

Unless the app is a live feed or something i don't see the point.

btw, most people are not on 56K anymore.
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Any what are the chances of a a actual server doing other things too when it is processing your script?[/QUOTE]Like what, Processing other requests? Updating the clock? Disk caching? Stepping outside for a smoke?

But yes, running the thing on an actual server doing nothing else would give us a better, but by no means normative, answer.
Copy linkTweet thisAlerts:
@bokehJun 23.2006 — Here's my take on it... You have one combination missing; you need a function with nothing in it to benchmark your own timing system. If you subtract out the time the timing function takes the discrepancy becomes much clearer. Here's what I added:[code=php]<?php
header("Content-Type: text/plain");
function no_string()
{
$start = microtime(TRUE);
return(microtime(TRUE) - $start);
}

function single_quote()
{
$start = microtime(TRUE);
$string = 'This is a test. It is only a test. Testing 1, 2, 3. '.
'This has been a test. It was only a test. 3, 2, 1, the end.';
return(microtime(TRUE) - $start);
}

function double_quote()
{
$start = microtime(TRUE);
$string = "This is a test. It is only a test. Testing 1, 2, 3. ".
"This has been a test. It was only a test. 3, 2, 1, the end.";
return(microtime(TRUE) - $start);
}

function heredoc_str()
{
$start = microtime(TRUE);
$string = <<<EOD
This is a test. It is only a test. Testing 1, 2, 3.
This has been a test. It was only a test. 3, 2, 1, the end.
EOD;
return(microtime(TRUE) - $start);
}
$seed = microtime(TRUE); // because first call is always slow
$methods = array('no_string', 'single_quote', 'double_quote', 'heredoc_str');
for($ix=0; $ix<200; $ix++)
{
array_rand($methods);
foreach($methods as $method)
{
$times[$method][] = call_user_func($method);
}
}
$methods = array('no_string', 'single_quote', 'double_quote', 'heredoc_str');
foreach($methods as $method)
{
$averages[$method] = sprintf("%01.6f",
array_sum($times[$method]) / count($times[$method]));
}
echo "Averages (seconds):n";
print_r($averages);
echo "nRaw Data:n";
print_r($times); // show raw data
?>[/code]
I ran this on my dedicated server and pulled on it from another machine on the LAN. Here is the result.[CODE]Averages (seconds):
Array
(
[no_string] => 0.000004
[single_quote] => 0.000006
[double_quote] => 0.000005
[heredoc_str] => 0.000018
)[/CODE]
Every time I ran it I got more or less the same result. Looking at this single quote take twice as long as doubles, and heredoc 14x longer.
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Every time I ran it I got more or less the same result. Looking at this single quote take twice as long as doubles, and heredoc 14x longer.[/QUOTE]How can that be? It can't take longer to do less.
Copy linkTweet thisAlerts:
@bokehJun 23.2006 — http://bokehman.com/benchmark/

Well a couple of things here, first a single quoted string is parsed and not just to look for single quotes so there is probably little difference in the work being done. Also in Nogdog's example although there is a double quoted string there is no work to do within the string. We are only testing runtime here. PHP is a compiled language and there may be a much larger deficit in the elapsed time between the two types at compile time than we are seeing here. Also how does the compiler treat a double quoted string if it contains no work? Personally I have no idea as to the answer to any of these questions, only that the runtime test indicates the double quoted string runs faster.
Copy linkTweet thisAlerts:
@NogDogauthorJun 23.2006 — Perhaps the PHP compiler optimizes the functions: it sees that there is no variable-interpolation involved within the double_quote() function and so optimizes it to just doing a simple string assignment. Maybe that optimized machine code is actually more efficient than that used by the single-quotes part of the compiler (perhaps done by different developers, even)? If that should be the case, then I guess my test approach would not be valid for what I was attempting to measure.
Copy linkTweet thisAlerts:
@tosbournJun 24.2006 — This question may seem a little irrelevant, but based on these results, would it be better to escape single quotes or mix single and double quotes up when the string contains " or '?
Copy linkTweet thisAlerts:
@NogDogauthorJun 24.2006 — Most of the time I'm less worried about saving a few milliseconds than I am about making my code easy to maintain, so I tend to opt for the quotation and/or concatenation method that makes my code easiest to read (and therefore modify). In fact, when things get messy, I'll use sprintf() or printf() to glue everything together in a string.
Copy linkTweet thisAlerts:
@bokehJun 24.2006 — This question may seem a little irrelevant, but based on these results, would it be better to escape single quotes or mix single and double quotes up when the string contains " or '?[/QUOTE]I think looking at these result shows that the host hardware has a much greater influence over the speed your script runs at than the type of quotes you use.
×

Success!

Help @NogDog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...