My PHP Performance Benchmarks

PHP version 5.2.17 is running on this server. The benchmarks are done live. Reload the page to get fresh numbers. You are free to use the source for whatever you want. Giving credits to me (Thiemo Mättig) would be nice.

Please note that these are micro benchmarks. Micro benchmarks are stupid. I created this comparison to learn something about PHP and how the PHP compiler works. This can not be used to compare PHP versions or servers.

Check if a String is empty

Method Undefined Null False Empty string String "0" String "1" Long string Summary Index
if (!$var)4 ms1 ms1 ms1 ms1 ms>0 ms>0 ms7 ms195
if (empty($var))1 ms1 ms1 ms1 ms1 ms>0 ms>0 ms5 ms135
if ($var == "")3 ms>0 ms>0 ms1 ms>0 ms>0 ms42 ms47 ms1388
if ("" == $var)2 ms>0 ms>0 ms1 ms>0 ms>0 ms>0 ms5 ms144
if ($var === "")2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms3 ms100
if ("" === $var)2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms4 ms112
if (strcmp($var, "") == 0)3 ms2 ms2 ms1 ms1 ms1 ms1 ms11 ms331
if (strcmp("", $var) == 0)3 ms2 ms2 ms1 ms1 ms1 ms1 ms11 ms329
if (strlen($var) == 0)3 ms1 ms1 ms1 ms1 ms1 ms1 ms9 ms268
if (!strlen($var))3 ms1 ms1 ms1 ms1 ms1 ms1 ms9 ms262

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 ms1 ms2 ms5 ms100
!strcmp($a, $b)4 ms3 ms4 ms11 ms201
strcmp($a, $b) == 04 ms3 ms4 ms11 ms214
strcmp($a, $b) === 04 ms3 ms4 ms11 ms200
strcasecmp($a, $b) === 08 ms3 ms8 ms19 ms353

My conclusion: Use what fits your needs.

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 ms1 ms1 ms1 ms4 ms100
strpos($haystack, $needle) !== false1 ms1 ms1 ms1 ms4 ms102
strstr($haystack, $needle) !== false1 ms1 ms1 ms1 ms4 ms109
stristr($haystack, $needle)2 ms2 ms2 ms2 ms8 ms193
preg_match("/$needle/", $haystack)2 ms2 ms2 ms2 ms8 ms190
preg_match("/$needle/i", $haystack)2 ms2 ms2 ms2 ms8 ms192
preg_match("/$needle/S", $haystack)2 ms2 ms2 ms2 ms8 ms189
ereg($needle, $haystack)2 ms2 ms9 ms17 ms30 ms740

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 ms1 ms1 ms1 ms6 ms157
strncmp($haystack, "Test", 4) === 01 ms1 ms1 ms1 ms4 ms101
strncasecmp($haystack, $needle, strlen($needle)) === 01 ms2 ms2 ms2 ms6 ms164
strpos($haystack, $needle) === 01 ms1 ms1 ms1 ms4 ms100
substr($haystack, 0, strlen($needle)) === $needle2 ms2 ms2 ms2 ms6 ms167
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 02 ms2 ms2 ms2 ms9 ms241
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack)3 ms3 ms3 ms3 ms12 ms319

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 ms2 ms2 ms2 ms8 ms100
substr($haystack, -strlen($needle)) === $needle2 ms2 ms3 ms3 ms9 ms116
strcmp(substr($haystack, -strlen($needle)), $needle) === 03 ms4 ms3 ms4 ms14 ms184
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack)6 ms5 ms3 ms4 ms18 ms235

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 ms2 ms2 ms3 ms10 ms100
preg_replace("/$search/", $replace, $subject)4 ms5 ms3 ms3 ms15 ms155
preg_replace("/$search/S", $replace, $subject)3 ms3 ms3 ms3 ms13 ms127
ereg_replace($search, $replace, $subject)3 ms6 ms12 ms20 ms41 ms417
strtr($subject, $array)18 ms17 ms12 ms12 ms59 ms601

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 ms>0 ms>0 ms>0 ms1 ms100
preg_replace('/^,*|,*$/', "", $string)7 ms7 ms7 ms7 ms29 ms3899
preg_replace('/^,*|,*$/m', "", $string)12 ms12 ms12 ms12 ms47 ms6425
preg_replace('/^,+|,+$/', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms245
preg_replace('/^,+|,+$/m', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms237
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …))1 ms1 ms1 ms1 ms3 ms409

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 ms1 ms7 ms10 ms100
split(",", $string)1 ms2 ms40 ms43 ms444
preg_split("/,/", $string)2 ms2 ms11 ms15 ms152
preg_match_all('/[^,]+/', $string, $matches)2 ms3 ms18 ms23 ms239

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++)42 ms5846
for ($i = 0, $count = count($array); $i < $count; $i++)1 ms141
for ($i = count($array) - 1; $i >= 0; $i--)1 ms136
for ($i = count($array) - 1; $i >= 0; --$i)1 ms131
$i = count($array); while ($i--)1 ms100

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

Get Elements from an Array

Method Summary Index
$array[0]33 ms100
$array['key']33 ms100

My conclusion: I like associative arrays.

Implode an Array

Method Summary Index
implode(" ", $array)5 ms100
"$array[0] $array[1] $array[2]"5 ms111
$array[0] . " " . $array[1] . " " . $array[2]5 ms103
sprintf("%s %s %s", $array[0], $array[1], $array[2])10 ms216
vsprintf("%s %s %s", $array)12 ms254

My conclusion: String concatenation is a cheap operation in PHP. Don't waste your time benchmarking this.

The single vs. double Quotes Myth

Method Summary Index
'contains no dollar signs'1 ms109
"contains no dollar signs"1 ms109
'$variables $are $not $replaced'1 ms108
"\$variables \$are \$not \$replaced"1 ms100
"$variables $are $replaced"7 ms1263
$variables . ' ' . $are . ' ' . $replaced8 ms1407
$variables . " " . $are . " " . $replaced8 ms1403

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 October 2012
More PHP experiments »