[nycphp-talk] single quote vs. double quote
Rolan Yang
rolan at omnistep.com
Tue Apr 10 12:52:23 EDT 2007
Hrm.. I just ran a real world test and the results were drastically
different.
With single quotes, it took about 3.4 seconds to complete.
With double quotes, 22 seconds on average.
My setup:
# php -v
PHP 5.1.6 (cli) (built: Nov 3 2006 07:27:53)
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
AMD Athlon(tm) XP 2200+
1GB RAM
It would be interesting for you guys to share your results too.
Here is the code. Run it from the command line with: "php -f quotetest.php"
or load it in your browser.
<?php
// Test speed of double quotes vs single quotes
// Rolan Yang rolan at omnistep.com 4/10/2007
$insertthis="abcdefg";
if (isset($_SERVER['HTTP_USER_AGENT'])) {$newline="<br>\n";} else
{$newline="\n";}
$start=microtime(TRUE);
print "Start: $start{$newline}";
for ($x=1;$x<5000000;$x++) {
// comment out one of the two lines below when testing
#$test="aerg;jia;rgjia;erigja;ergja;rga;erigjae;rigjae;riogja;eriogjae;rgjae;riojga;erji{$insertthis}";
$test='aerg;jia;rgjia;erigja;ergja;rga;erigjae;rigjae;riogja;eriogjae;rgjae;riojga;erji'.$insertthis;
}
$end=microtime(TRUE);
print "End: $end{$newline}Total=".($end-$start).$newline;
?>
~Rolan
Paul M Jones wrote:
>
> On Apr 10, 2007, at 11:05 AM, Paul M Jones wrote:
>
>> On Apr 10, 2007, at 11:03 AM, Rahmin Pavlovic wrote:
>>
>>> Does anyone know approximately how much longer double-quotes take to
>>> parse
>>> in a controlled environment?
>>
>> My understanding is "little if any"; recent versions of PHP 4 have
>> eliminated the time-difference in single/double quote parsing.
>
> In fact, here's a blog entry about it from Sara Golemon:
>
>
> <http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html>
>
>
> See comments 4 and 4.1 for the specific answer:
>
>> Definitely a very interesting article.
>> It would also be nice to know whether there is a difference between
>> single and double quotes because there is always a lot of discussion
>> going on about that, too. (and i don't know how to find out the
>> opcodes and stuff ;-) )
>> #4 Jan on 2006-06-19 10:19 (Reply)
> ...
>> Short version: No. The margin between single and double quoted is on
>> the order of nanoseconds. The only thing that gives single-quote any
>> kind of edge over double-quote (assuming they're both
>> constant/non-interpolated strings) is the fact that double quoted
>> strings are subject to more substitution rules (\r\n\t\xFF\012) and
>> so have to spend an extra clock-cycle or two on scanning.
>>
>> Just like so many compile-time bottlenecks though, once you introduce
>> an opcode cache, this distinction vanishes.
>> #4.1 Sara Golemon on 2006-06-19 11:02 (Reply)
>
>
>
> --
>
> Paul M. Jones <http://paul-m-jones.com>
>
> Solar: Simple Object Library and Application Repository
> for PHP5. <http://solarphp.com>
>
> Join the Solar community wiki! <http://solarphp.org>
>
> Savant: The simple, elegant, and powerful solution for
> templates in PHP. <http://phpsavant.com>
>
>
>
>
>
>
>
> --
>
> Paul M. Jones <http://paul-m-jones.com>
>
> Solar: Simple Object Library and Application Repository
> for PHP5. <http://solarphp.com>
>
> Join the Solar community wiki! <http://solarphp.org>
>
> Savant: The simple, elegant, and powerful solution for
> templates in PHP. <http://phpsavant.com>
>
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
More information about the talk
mailing list