My PHP Performance Benchmarks

PHP version 5.2.13 is running on this server.

Check if a String is empty

Method Undefined Null False Empty string String "0" String "1" Long string Summary Index
if (!$var)2.4 ms0.4 ms0.4 ms0.4 ms0.4 ms0.2 ms0.2 ms4.3 ms136
if (empty($var))0.4 ms0.5 ms0.5 ms0.5 ms0.5 ms0.3 ms0.3 ms3.1 ms100
if ($var == "")2.6 ms0.4 ms0.4 ms0.5 ms0.5 ms0.5 ms38.0 ms43.0 ms1371
if ("" == $var)2.6 ms0.5 ms0.5 ms0.5 ms0.3 ms0.3 ms0.4 ms5.1 ms161
if ($var === "")2.1 ms0.2 ms0.2 ms0.5 ms0.2 ms0.2 ms0.2 ms3.5 ms111
if ("" === $var)2.1 ms0.2 ms0.2 ms0.4 ms0.2 ms0.2 ms0.2 ms3.5 ms112
if (strcmp($var, "") == 0)3.7 ms1.5 ms1.8 ms1.3 ms1.2 ms1.6 ms1.1 ms12.3 ms391
if (strcmp("", $var) == 0)3.8 ms1.6 ms1.7 ms1.3 ms1.1 ms1.1 ms1.1 ms11.8 ms375
if (strlen($var) == 0)3.4 ms1.2 ms1.3 ms1.0 ms0.8 ms0.8 ms0.8 ms9.4 ms301
if (!strlen($var))3.2 ms1.1 ms1.3 ms0.9 ms0.7 ms0.8 ms0.7 ms8.8 ms280

My conclusion: In most cases, use empty() because it does not trigger a warning when used with undefined variables. Note that empty("0") returns true. Use strlen() if you want to detect "0". Try to avoid == at all because it may cause strange behaviour (e.g. "9a" == 9 returns true). Prefer === over == and !== over != if possible because it does compare the variable types in addition to the contents.

Compare two Strings

Method Equal First character not equal Last character not equal Summary Index
$a == $b2.1 ms1.4 ms1.9 ms5.3 ms100
!strcmp($a, $b)3.9 ms3.2 ms3.8 ms10.8 ms203
strcmp($a, $b) == 03.9 ms3.3 ms3.9 ms11.2 ms210
strcmp($a, $b) === 03.7 ms3.5 ms3.8 ms11.0 ms205
strcasecmp($a, $b) === 08.0 ms3.1 ms8.0 ms19.0 ms356

My conclusion: ?

Check if a String contains another String

Method Not found Found at the start Found in the middle Found at the end Summary Index
strstr($haystack, $needle)1.0 ms1.0 ms1.1 ms1.1 ms4.2 ms102
strpos($haystack, $needle) !== false0.9 ms1.0 ms1.1 ms1.1 ms4.1 ms100
strstr($haystack, $needle) !== false0.9 ms1.1 ms1.2 ms1.2 ms4.4 ms108
stristr($haystack, $needle)1.8 ms2.2 ms2.8 ms2.1 ms8.9 ms217
preg_match("/$needle/", $haystack)1.9 ms1.8 ms2.0 ms2.2 ms7.9 ms194
preg_match("/$needle/i", $haystack)1.9 ms1.9 ms2.1 ms2.3 ms8.2 ms200
preg_match("/$needle/S", $haystack)1.9 ms1.8 ms2.1 ms2.2 ms8.0 ms196
ereg($needle, $haystack)2.2 ms2.2 ms9.5 ms16.8 ms30.6 ms748

My conclusion: It does not matter if you use strstr() or strpos(). Use the preg…() functions only if you need the power of regular expressions. Never use the ereg…() functions.

Check if a String starts with another String

Method Not found Found at the start Found in the middle Found at the end Summary Index
strncmp($haystack, $needle, strlen($needle)) === 01.5 ms1.4 ms1.4 ms1.4 ms5.7 ms149
strncmp($haystack, "Test", 4) === 01.0 ms1.0 ms1.0 ms1.0 ms4.0 ms103
strncasecmp($haystack, $needle, strlen($needle)) === 01.4 ms1.4 ms1.4 ms1.4 ms5.6 ms146
strpos($haystack, $needle) === 01.0 ms0.9 ms1.0 ms1.0 ms3.8 ms100
substr($haystack, 0, strlen($needle)) === $needle1.6 ms1.6 ms1.6 ms1.6 ms6.4 ms165
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 02.2 ms2.2 ms2.3 ms2.2 ms8.9 ms232
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack)2.9 ms3.1 ms2.9 ms2.9 ms11.8 ms306

My conclusion: strpos() is very fast and can be used in almost all cases. strncmp() is good if you are looking for a constant length needle.

Check if a String ends with another String

Method Not found Found at the start Found in the middle Found at the end Summary Index
substr($haystack, strlen($haystack) - strlen($needle)) === $needle2.0 ms2.0 ms2.3 ms2.3 ms8.5 ms136
substr($haystack, -strlen($needle)) === $needle1.6 ms1.6 ms1.6 ms1.6 ms6.3 ms100
strcmp(substr($haystack, -strlen($needle)), $needle) === 02.2 ms2.2 ms2.2 ms2.2 ms8.8 ms141
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack)3.3 ms3.7 ms3.6 ms3.7 ms14.2 ms227

My conclusion: Using substr() with a negative position is a good trick.

Replace a String inside another String

Method Not found Found at the start Found in the middle Found at the end Summary Index
str_replace($search, $replace, $subject)1.8 ms1.9 ms1.8 ms1.9 ms7.5 ms100
preg_replace("/$search/", $replace, $subject)2.9 ms3.2 ms3.4 ms3.3 ms12.8 ms171
preg_replace("/$search/S", $replace, $subject)2.7 ms3.3 ms3.3 ms3.2 ms12.5 ms167
ereg_replace($search, $replace, $subject)3.4 ms5.6 ms12.1 ms19.0 ms40.1 ms535

My conclusion: Never use the ereg…() functions.

Trim Characters from the Beginning and End of a String

Method Not found Found at start Found at end Found at both sides Summary Index
trim($string, ",")0.2 ms0.2 ms0.2 ms0.2 ms0.8 ms100
preg_replace('/^,*|,*$/', "", $string)6.8 ms6.9 ms6.8 ms6.9 ms27.4 ms3409
preg_replace('/^,*|,*$/m', "", $string)11.4 ms11.3 ms11.3 ms11.3 ms45.3 ms5637
preg_replace('/^,+|,+$/', "", $string)0.5 ms0.5 ms0.5 ms0.5 ms1.9 ms232
preg_replace('/^,+|,+$/m', "", $string)0.4 ms0.4 ms0.5 ms0.5 ms1.8 ms224
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …))0.8 ms0.8 ms0.8 ms0.8 ms3.1 ms381

My conclusion: Always benchmark your regular expressions! In this case, with .* you also replace nothing with nothing which takes time because there is a lot of “nothing” in every string.

Split a String into an Array

Method Empty string Single occurrence Multiple occurrences Summary Index
explode(",", $string)1.1 ms1.2 ms7.6 ms10.0 ms100
split(",", $string)1.2 ms1.9 ms40.4 ms43.5 ms436
preg_split("/,/", $string)1.6 ms1.9 ms11.4 ms14.8 ms149
preg_match_all('/[^,]+/', $string, $matches)2.1 ms3.5 ms18.5 ms24.1 ms242

My conclusion: Don't use split(). It's deprecated in PHP 5.3 and will be removed in PHP 6.

Loop a numerical indexed Array of Strings

Method Summary Index
for ($i = 0; $i < count($array); $i++)43.9 ms6044
for ($i = 0, $count = count($array); $i < $count; $i++)1.0 ms139
for ($i = count($array) - 1; $i >= 0; $i--)1.0 ms131
for ($i = count($array) - 1; $i >= 0; --$i)0.9 ms129
$i = count($array); while ($i--)0.7 ms100

My conclusion: count() is horribly slow. Always precalculate it, if possible.

Get Elements from an Array

Method Summary Index
$array[0]33.5 ms100
$array['key']34.4 ms103

My conclusion: I like associative arrays.

The single vs. double Quotes Myth

Method Summary Index
'contains no dollar signs'0.7 ms108
"contains no dollar signs"0.7 ms110
'$variables $are $not $replaced'0.6 ms100
"\$variables \$are \$not \$replaced"0.6 ms103
"$variables $are $replaced"7.5 ms1235
$variables . ' ' . $are . ' ' . $replaced9.1 ms1497
$variables . " " . $are . " " . $replaced8.8 ms1444

My conclusion: It does not matter if you use single or double quotes at all. The inclusion of variables has a measurable effect, but that's independent from the quotes.

© Thiemo Mättig, created in September 2008, updated in March 2010
More PHP experiments »