分享

My Collection of PHP Performance Benchmarks

 nywrb 2012-10-29

My PHP Performance Benchmarks

PHP version 5.2.13 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)2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms4 ms125
if (empty($var))>0 ms1 ms1 ms1 ms1 ms>0 ms>0 ms3 ms103
if ($var == "")2 ms>0 ms>0 ms1 ms1 ms>0 ms41 ms45 ms1358
if ("" == $var)2 ms>0 ms>0 ms1 ms>0 ms>0 ms>0 ms5 ms145
if ($var === "")2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms3 ms100
if ("" === $var)2 ms>0 ms>0 ms1 ms>0 ms>0 ms>0 ms4 ms128
if (strcmp($var, "") == 0)4 ms2 ms2 ms1 ms1 ms1 ms1 ms12 ms359
if (strcmp("", $var) == 0)4 ms2 ms2 ms1 ms1 ms1 ms1 ms12 ms346
if (strlen($var) == 0)3 ms1 ms1 ms1 ms1 ms1 ms1 ms9 ms280
if (!strlen($var))3 ms1 ms1 ms1 ms1 ms1 ms1 ms9 ms256

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 ms205
strcmp($a, $b) == 04 ms4 ms4 ms12 ms218
strcmp($a, $b) === 04 ms3 ms6 ms14 ms259
strcasecmp($a, $b) === 012 ms3 ms8 ms23 ms423

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 ms100
strstr($haystack, $needle) !== false1 ms1 ms1 ms1 ms4 ms108
stristr($haystack, $needle)2 ms2 ms2 ms2 ms8 ms195
preg_match("/$needle/", $haystack)2 ms2 ms2 ms2 ms8 ms196
preg_match("/$needle/i", $haystack)2 ms2 ms2 ms2 ms8 ms203
preg_match("/$needle/S", $haystack)2 ms2 ms2 ms2 ms8 ms201
ereg($needle, $haystack)2 ms2 ms10 ms18 ms33 ms800

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 ms150
strncmp($haystack, "Test", 4) === 01 ms1 ms1 ms1 ms4 ms104
strncasecmp($haystack, $needle, strlen($needle)) === 02 ms2 ms2 ms2 ms6 ms168
strpos($haystack, $needle) === 01 ms1 ms1 ms1 ms4 ms100
substr($haystack, 0, strlen($needle)) === $needle2 ms2 ms2 ms2 ms6 ms165
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 02 ms2 ms2 ms2 ms9 ms234
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack)3 ms3 ms3 ms3 ms13 ms330

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 ms126
substr($haystack, -strlen($needle)) === $needle2 ms2 ms2 ms2 ms6 ms100
strcmp(substr($haystack, -strlen($needle)), $needle) === 02 ms2 ms2 ms2 ms9 ms138
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack)3 ms4 ms4 ms4 ms14 ms224

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)2 ms2 ms2 ms2 ms7 ms100
preg_replace("/$search/", $replace, $subject)3 ms3 ms4 ms3 ms13 ms180
preg_replace("/$search/S", $replace, $subject)3 ms3 ms3 ms3 ms13 ms179
ereg_replace($search, $replace, $subject)4 ms6 ms14 ms21 ms44 ms599

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 ms27 ms3706
preg_replace('/^,*|,*$/m', "", $string)11 ms11 ms11 ms11 ms45 ms6041
preg_replace('/^,+|,+$/', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms253
preg_replace('/^,+|,+$/m', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms241
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …))1 ms1 ms1 ms1 ms3 ms422

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 ms44 ms47 ms490
preg_split("/,/", $string)2 ms2 ms12 ms15 ms161
preg_match_all('/[^,]+/', $string, $matches)2 ms4 ms18 ms24 ms253

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 ms5793
for ($i = 0, $count = count($array); $i < $count; $i++)1 ms139
for ($i = count($array) - 1; $i >= 0; $i--)1 ms156
for ($i = count($array) - 1; $i >= 0; --$i)1 ms152
$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 ms101
$array['key']33 ms100

My conclusion: I like associative arrays.

Implode an Array

Method Summary Index
implode(" ", $array)5 ms102
"$array[0] $array[1] $array[2]"5 ms100
$array[0] . " " . $array[1] . " " . $array[2]5 ms104
sprintf("%s %s %s", $array[0], $array[1], $array[2])10 ms205
vsprintf("%s %s %s", $array)12 ms244

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 ms113
"contains no dollar signs"1 ms104
'$variables $are $not $replaced'1 ms110
"\$variables \$are \$not \$replaced"1 ms100
"$variables $are $replaced"8 ms1230
$variables . ' ' . $are . ' ' . $replaced9 ms1421
$variables . " " . $are . " " . $replaced9 ms1413

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 ?

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多