My PHP Performance Benchmarks

PHP version 4.4.8 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)3.1 ms1.1 ms1.1 ms1.1 ms1.1 ms0.6 ms0.6 ms8.6 ms128
if (empty($var))1.1 ms51.1 ms1.1 ms1.1 ms1.1 ms0.6 ms0.6 ms56.6 ms845
if ($var == "")3.2 ms1.2 ms271.2 ms1.2 ms0.9 ms0.9 ms2,454.5 ms2,732.9 ms40844
if ("" == $var)403.2 ms1.1 ms1.2 ms845.2 ms0.7 ms0.7 ms0.7 ms1,252.8 ms18723
if ($var === "")2.6 ms0.6 ms0.6 ms1.2 ms0.6 ms0.6 ms0.6 ms6.7 ms100
if ("" === $var)691.1 ms0.6 ms0.6 ms1.2 ms0.6 ms0.6 ms0.6 ms695.2 ms10390
if (strcmp($var, "") == 0)4.3 ms822.3 ms2.2 ms2.1 ms1.5 ms1.5 ms352.2 ms1,186.1 ms17726
if (strcmp("", $var) == 0)14.4 ms2.4 ms2.3 ms632.2 ms1.5 ms1.5 ms1.5 ms655.9 ms9802
if (strlen($var) == 0)4.0 ms182.0 ms2.0 ms1.8 ms1.3 ms1.3 ms1.3 ms193.5 ms2892
if (!strlen($var))573.8 ms1.9 ms1.9 ms1.7 ms1.2 ms391.2 ms1.2 ms972.8 ms14539

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.

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.2 ms2.1 ms1.7 ms1.8 ms6.8 ms100
strpos($haystack, $needle) !== false1.5 ms1,072.1 ms2.1 ms2.2 ms1,077.8 ms15903
strstr($haystack, $needle) !== false1.5 ms2.4 ms402.1 ms2.2 ms408.2 ms6023
stristr($haystack, $needle)2.9 ms3.8 ms313.5 ms3.6 ms323.8 ms4777
preg_match("/$needle/", $haystack)2.1 ms672.5 ms130.3 ms2.8 ms807.7 ms11918
preg_match("/$needle/i", $haystack)2.2 ms2.5 ms292.7 ms2.9 ms300.4 ms4432
preg_match("/$needle/S", $haystack)2.1 ms2.5 ms531.6 ms2.8 ms539.0 ms7953
ereg($needle, $haystack)2.9 ms53.3 ms650.1 ms106.9 ms813.3 ms12000

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)) === 02.1 ms2.1 ms2.1 ms2.1 ms8.4 ms139
strncmp($haystack, "Test", 4) === 0101.7 ms1.7 ms338.1 ms1.7 ms443.2 ms7269
strncasecmp($haystack, $needle, strlen($needle)) === 02.1 ms2.1 ms2.1 ms32.1 ms38.4 ms629
strpos($haystack, $needle) === 01.5 ms1.5 ms1.5 ms1.6 ms6.1 ms100
substr($haystack, 0, strlen($needle)) === $needle2.3 ms232.3 ms2.3 ms2.3 ms239.3 ms3924
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 03.0 ms33.0 ms3.1 ms3.0 ms42.2 ms692
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack)45.0 ms5.0 ms114.9 ms4.9 ms169.9 ms2786

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)) === $needle3.0 ms3.2 ms3.0 ms53.6 ms62.8 ms117
substr($haystack, -strlen($needle)) === $needle2.5 ms2.4 ms132.5 ms2.4 ms139.8 ms261
strcmp(substr($haystack, -strlen($needle)), $needle) === 03.3 ms3.7 ms43.3 ms3.3 ms53.5 ms100
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack)115.8 ms5.8 ms146.0 ms5.9 ms273.6 ms511

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)3.0 ms3.6 ms3.2 ms1,073.2 ms1,083.0 ms2988
preg_replace("/$search/", $replace, $subject)3.7 ms24.3 ms4.1 ms4.1 ms36.2 ms100
preg_replace("/$search/S", $replace, $subject)45.5 ms4.2 ms4.1 ms4.1 ms58.0 ms160
ereg_replace($search, $replace, $subject)4.7 ms6.8 ms13.1 ms19.5 ms44.2 ms122

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.4 ms0.4 ms0.4 ms0.4 ms1.8 ms100
preg_replace('/^,*|,*$/', "", $string)10.4 ms10.4 ms10.4 ms10.4 ms41.8 ms2383
preg_replace('/^,*|,*$/m', "", $string)16.6 ms16.6 ms262.9 ms151.8 ms447.9 ms25569
preg_replace('/^,+|,+$/', "", $string)0.8 ms0.8 ms0.8 ms0.8 ms3.1 ms178
preg_replace('/^,+|,+$/m', "", $string)0.8 ms0.8 ms40.8 ms0.8 ms43.2 ms2464
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …))1.2 ms1.2 ms1.2 ms1.2 ms4.9 ms280

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.5 ms1.8 ms213.8 ms217.2 ms766
split(",", $string)1.6 ms351.8 ms1,481.5 ms1,834.9 ms6474
preg_split("/,/", $string)1.9 ms2.4 ms24.1 ms28.3 ms100
preg_match_all('/[^,]+/', $string, $matches)2.8 ms4.5 ms28.3 ms35.6 ms126

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++)95.0 ms4483
for ($i = 0, $count = count($array); $i < $count; $i++)3.5 ms164
for ($i = count($array) - 1; $i >= 0; $i--)2.7 ms128
for ($i = count($array) - 1; $i >= 0; --$i)2.6 ms123
$i = count($array); while ($i--)2.1 ms100

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

The single vs. double Quotes Myth

Method Summary Index
'contains no dollar signs'1.8 ms101
"contains no dollar signs"1.8 ms101
'$variables $are $not $replaced'1.8 ms100
"\$variables \$are \$not \$replaced"1.9 ms109
"$variables $are $replaced"16.1 ms909
$variables . ' ' . $are . ' ' . $replaced22.9 ms1292
$variables . " " . $are . " " . $replaced22.3 ms1258

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 »