Jump to content

PHP's string parsing performacne


owzim
 Share

Recommended Posts

I was just curios about how fast PHP handles the different types of string concatenation/parsing with variables, because I saw Ryan's style is double quotes and I was thinking, isn't that slower than single quote concatenation?

I was surprised because I remember to have learned that the additional parsing of double quote strings make the whole thing slower. I was wrong, here is my benchmark.

Thought it'd be interesting to know.

1000000 times per test

// initial object creation, only once
$fooObject = new stdClass;
$fooObject->foo = "Lorem ipsum dolor sit amet.";
$fooObject->bar = "Consectetur adipisicing elit.";
$fooObject->baz = "Sed do eiusmod tempor incididunt.";

double quotes

"foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}"

0.55859899520874 s


single quotes concat

'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz

0.61702013015747 s


sprintf

sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz)

1.6139810085297 s


array implode

implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz))

2.0107381343842 s


Code:

<?php $times = 1000000; ?>

<h1>PHP string concat benchmark</h1>
<p><?=$times?> times per test</p>
<code><pre>
$fooObject = new stdClass;
$fooObject->foo = "Lorem ipsum dolor sit amet.";
$fooObject->bar = "Consectetur adipisicing elit.";
$fooObject->baz = "Sed do eiusmod tempor incididunt.";
</pre></code><br>
<hr>

<?php
function microtime_float() {
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}

$fooObject = new stdClass;
$fooObject->foo = "Lorem ipsum dolor sit amet.";
$fooObject->bar = "Consectetur adipisicing elit.";
$fooObject->baz = "Sed do eiusmod tempor incididunt.";

$time_start = microtime_float();

for ($i=0; $i < $times; $i++) { 
	$someString = "foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}";
}

$time_end = microtime_float();
$time = $time_end - $time_start;
?>
<h2>double quote</h2>
<code>"foo: {$fooObject->foo}, bar: {$fooObject->bar}, baz: {$fooObject->baz}"</code><br><br>
<?=$time?> s<br><br>
<hr>
<?php

$time_start = microtime_float();

$someString = '';
for ($i=0; $i < $times; $i++) { 
	$someString = 'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz;
}

$time_end = microtime_float();
$time = $time_end - $time_start;
?>
<h2>single quote concat</h2>
<code>'foo: ' . $fooObject->foo . ', bar: ' . $fooObject->bar . ', baz: ' . $fooObject->baz</code><br><br>
<?=$time?> s<br><br>
<hr>
<?php

$time_start = microtime_float();

for ($i=0; $i < $times; $i++) { 
	$someString = sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz);
}

$time_end = microtime_float();
$time = $time_end - $time_start;
?>
<h2>sprintf</h2>
<code>sprintf("foo: %s, bar: %s, baz: %s", $fooObject->foo, $fooObject->bar, $fooObject->baz)</code><br><br>
<?=$time?> s<br><br>
<hr>
<?php

$time_start = microtime_float();

for ($i=0; $i < $times; $i++) { 
	$someString = implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz));
}

$time_end = microtime_float();
$time = $time_end - $time_start;
?>
<h2>array implode</h2>
<code>implode('', array('foo: ', $fooObject->foo, ', bar: ', $fooObject->bar, 'baz: ', $fooObject->baz))</code><br><br>
<?=$time?> s<br><br>
  • Like 3
Link to comment
Share on other sites

I could have sworn there was a discussion about this somewhere on the forums already, but I can't find it right now.

Here are a couple of external discussions:

http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php

http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes

A good collection of php comparisons:

http://www.phpbench.com/

I seem to see conflicting results for all these optimizations :)

  • Like 1
Link to comment
Share on other sites

I was just curios about how fast PHP handles the different types of string concatenation/parsing with variables, because I saw Ryan's style is double quotes and I was thinking, isn't that slower than single quote concatenation?

...

@owzim: I remember vague that this was real in the times when PHP version just had changed from 4.0.6 to 4.1.0. But its a long time ago :)

Unfortunately the code you provided for testing will not run with the oldtimers, otherwise I would have done a test with it. :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...