分类: Python/Ruby
2012-11-23 10:04:51
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 for whatever you want. Giving credits to me () 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 emptyMethod | 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 ms | 4 ms | 134 |
if (empty($var)) | >0 ms | 1 ms | 1 ms | 1 ms | 1 ms | >0 ms | >0 ms | 3 ms | 100 |
if ($var == "") | 2 ms | >0 ms | >0 ms | 1 ms | 1 ms | 1 ms | 41 ms | 46 ms | 1442 |
if ("" == $var) | 2 ms | >0 ms | >0 ms | 1 ms | 1 ms | >0 ms | >0 ms | 5 ms | 163 |
if ($var === "") | 2 ms | >0 ms | >0 ms | >0 ms | >0 ms | >0 ms | >0 ms | 3 ms | 105 |
if ("" === $var) | 3 ms | >0 ms | >0 ms | 1 ms | >0 ms | >0 ms | >0 ms | 5 ms | 151 |
if (strcmp($var, "") == 0) | 4 ms | 2 ms | 2 ms | 1 ms | 1 ms | 1 ms | 1 ms | 14 ms | 430 |
if (strcmp("", $var) == 0) | 4 ms | 2 ms | 2 ms | 2 ms | 1 ms | 1 ms | 1 ms | 13 ms | 417 |
if (strlen($var) == 0) | 3 ms | 1 ms | 1 ms | 1 ms | 1 ms | 1 ms | 1 ms | 10 ms | 308 |
if (!strlen($var)) | 3 ms | 1 ms | 1 ms | 1 ms | 1 ms | 1 ms | 1 ms | 9 ms | 293 |
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 StringsMethod | Equal | First character not equal | Last character not equal | Summary | Index |
---|---|---|---|---|---|
$a == $b | 2 ms | 1 ms | 2 ms | 5 ms | 100 |
!strcmp($a, $b) | 4 ms | 3 ms | 4 ms | 11 ms | 205 |
strcmp($a, $b) == 0 | 4 ms | 3 ms | 4 ms | 11 ms | 218 |
strcmp($a, $b) === 0 | 4 ms | 3 ms | 5 ms | 12 ms | 219 |
strcasecmp($a, $b) === 0 | 9 ms | 3 ms | 8 ms | 21 ms | 392 |
My conclusion: Use what fits your needs.
Check if a String contains another StringMethod | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
---|---|---|---|---|---|---|
strstr($haystack, $needle) | 1 ms | 1 ms | 1 ms | 1 ms | 5 ms | 100 |
strpos($haystack, $needle) !== false | 1 ms | 1 ms | 1 ms | 2 ms | 5 ms | 111 |
strstr($haystack, $needle) !== false | 1 ms | 1 ms | 1 ms | 1 ms | 5 ms | 109 |
stristr($haystack, $needle) | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 178 |
preg_match("/$needle/", $haystack) | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 167 |
preg_match("/$needle/i", $haystack) | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 175 |
preg_match("/$needle/S", $haystack) | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 166 |
ereg($needle, $haystack) | 2 ms | 2 ms | 10 ms | 18 ms | 32 ms | 688 |
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 StringMethod | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
---|---|---|---|---|---|---|
strncmp($haystack, $needle, strlen($needle)) === 0 | 1 ms | 1 ms | 1 ms | 1 ms | 6 ms | 139 |
strncmp($haystack, "Test", 4) === 0 | 1 ms | 1 ms | 1 ms | 1 ms | 5 ms | 109 |
strncasecmp($haystack, $needle, strlen($needle)) === 0 | 1 ms | 1 ms | 1 ms | 2 ms | 6 ms | 142 |
strpos($haystack, $needle) === 0 | 1 ms | 1 ms | 1 ms | 1 ms | 4 ms | 100 |
substr($haystack, 0, strlen($needle)) === $needle | 2 ms | 2 ms | 2 ms | 2 ms | 7 ms | 176 |
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0 | 2 ms | 2 ms | 3 ms | 3 ms | 10 ms | 246 |
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack) | 3 ms | 3 ms | 3 ms | 3 ms | 13 ms | 304 |
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 StringMethod | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
---|---|---|---|---|---|---|
substr($haystack, strlen($haystack) - strlen($needle)) === $needle | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 117 |
substr($haystack, -strlen($needle)) === $needle | 2 ms | 2 ms | 2 ms | 2 ms | 7 ms | 100 |
strcmp(substr($haystack, -strlen($needle)), $needle) === 0 | 3 ms | 3 ms | 4 ms | 4 ms | 14 ms | 196 |
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack) | 6 ms | 4 ms | 4 ms | 4 ms | 17 ms | 239 |
My conclusion: Using substr() with a negative position is a good trick.
Replace a String inside another StringMethod | Not found | Found at the start | Found in the middle | Found at the end | Summary | Index |
---|---|---|---|---|---|---|
str_replace($search, $replace, $subject) | 2 ms | 2 ms | 2 ms | 2 ms | 8 ms | 100 |
preg_replace("/$search/", $replace, $subject) | 3 ms | 4 ms | 4 ms | 4 ms | 14 ms | 183 |
preg_replace("/$search/S", $replace, $subject) | 3 ms | 3 ms | 5 ms | 3 ms | 14 ms | 190 |
ereg_replace($search, $replace, $subject) | 3 ms | 7 ms | 12 ms | 19 ms | 41 ms | 545 |
My conclusion: Never use the ereg…() functions.
Trim Characters from the Beginning and End of a StringMethod | Not found | Found at start | Found at end | Found at both sides | Summary | Index |
---|---|---|---|---|---|---|
trim($string, ",") | >0 ms | >0 ms | >0 ms | >0 ms | 1 ms | 100 |
preg_replace('/^,*|,*$/', "", $string) | 7 ms | 8 ms | 7 ms | 7 ms | 28 ms | 3566 |
preg_replace('/^,*|,*$/m', "", $string) | 12 ms | 11 ms | 11 ms | 11 ms | 45 ms | 5775 |
preg_replace('/^,+|,+$/', "", $string) | >0 ms | >0 ms | >0 ms | >0 ms | 2 ms | 243 |
preg_replace('/^,+|,+$/m', "", $string) | >0 ms | >0 ms | >0 ms | >0 ms | 2 ms | 237 |
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …)) | 1 ms | 1 ms | 1 ms | 1 ms | 3 ms | 420 |
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 ArrayMethod | Empty string | Single occurrence | Multiple occurrences | Summary | Index |
---|---|---|---|---|---|
explode(",", $string) | 1 ms | 1 ms | 8 ms | 10 ms | 100 |
split(",", $string) | 1 ms | 3 ms | 47 ms | 52 ms | 497 |
preg_split("/,/", $string) | 2 ms | 2 ms | 11 ms | 15 ms | 143 |
preg_match_all('/[^,]+/', $string, $matches) | 2 ms | 4 ms | 18 ms | 24 ms | 232 |
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 StringsMethod | Summary | Index |
---|---|---|
for ($i = 0; $i < count($array); $i++) | 43 ms | 6054 |
for ($i = 0, $count = count($array); $i < $count; $i++) | 1 ms | 141 |
for ($i = count($array) - 1; $i >= 0; $i--) | 1 ms | 158 |
for ($i = count($array) - 1; $i >= 0; --$i) | 1 ms | 155 |
$i = count($array); while ($i--) | 1 ms | 100 |
My conclusion: count() is horribly slow. Always precalculate it, if possible.
Get Elements from an ArrayMethod | Summary | Index |
---|---|---|
$array[0] | 40 ms | 118 |
$array['key'] | 34 ms | 100 |
My conclusion: I like associative arrays.
Implode an ArrayMethod | Summary | Index |
---|---|---|
implode(" ", $array) | 5 ms | 113 |
"$array[0] $array[1] $array[2]" | 5 ms | 101 |
$array[0] . " " . $array[1] . " " . $array[2] | 5 ms | 100 |
sprintf("%s %s %s", $array[0], $array[1], $array[2]) | 10 ms | 217 |
vsprintf("%s %s %s", $array) | 12 ms | 253 |
My conclusion: String concatenation is a cheap operation in PHP. Don't waste your time benchmarking this.
The single vs. double Quotes MythMethod | Summary | Index |
---|---|---|
'contains no dollar signs' | 1 ms | 100 |
"contains no dollar signs" | 1 ms | 107 |
'$variables $are $not $replaced' | 1 ms | 102 |
"\$variables \$are \$not \$replaced" | 1 ms | 107 |
"$variables $are $replaced" | 7 ms | 1192 |
$variables . ' ' . $are . ' ' . $replaced | 9 ms | 1445 |
$variables . " " . $are . " " . $replaced | 8 ms | 1402 |
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.