You are on page 1of 4

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 Mttig) 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.

Method Undefined Null False Empty String String Long Summary Index
string "0" "1" string
if (!$var) 2 ms >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms 4 ms 147
if >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms 3 ms 100
(empty($var))
if ($var == "") 2 ms >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms 5 ms 173
if ("" == $var) 2 ms >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms 4 ms 157
if ($var === 2 ms >0 ms >0 ms 1 ms >0 ms >0 ms >0 ms 3 ms 122
"")
if ("" === 2 ms >0 ms >0 ms >0 ms >0 ms >0 ms >0 ms 3 ms 114
$var)
if 4 ms 1 ms 2 ms 1 ms 1 ms 1 ms 1 ms 11 ms 383
(strcmp($var,
"") == 0)
if (strcmp("", 4 ms 1 ms 1 ms 1 ms 1 ms 1 ms 1 ms 10 ms 371
$var) == 0)
if 3 ms 1 ms 1 ms 1 ms 1 ms 1 ms 1 ms 9 ms 304
(strlen($var)
== 0)
if 3 ms 1 ms 1 ms 1 ms 1 ms 1 ms 1 ms 8 ms 285
(!strlen($var))

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.

Method Empty array 100 elements Summary Index


count($array) === 0 //by reference 4 ms 40 ms 44 ms 3362
count($array) === 0 //by value 3 ms 3 ms 5 ms 406
$array === array() 1 ms 1 ms 3 ms 223
empty($array) 1 ms 1 ms 2 ms 141
(bool)$array 1 ms 1 ms 1 ms 100

My conclusion: Why count if you don't care about the exact number?

Method Equal First character not Last character not Summary Index
equal equal
$a == $b 1 ms 1 ms 1 ms 3 ms 135
$a === $b 1 ms 1 ms 1 ms 2 ms 100
!strcmp($a, $b) 3 ms 3 ms 3 ms 8 ms 323
strcmp($a, $b) == 0 3 ms 3 ms 3 ms 8 ms 343
strcmp($a, $b) === 0 3 ms 2 ms 3 ms 8 ms 317
strcasecmp($a, $b) === 8 ms 3 ms 8 ms 19 ms 781
0
My conclusion: Use what fits your needs.

Method Not Found at Found in Found at Summary Index


found the start the middle the end
strstr($haystack, $needle) 1 ms 1 ms 1 ms 1 ms 4 ms 107
strpos($haystack, $needle) 1 ms 1 ms 1 ms 1 ms 3 ms 100
!== false
strstr($haystack, $needle) 1 ms 1 ms 1 ms 1 ms 4 ms 112
!== false
stristr($haystack, $needle) 2 ms 2 ms 2 ms 2 ms 7 ms 229
preg_match("/$needle/", 2 ms 2 ms 2 ms 2 ms 8 ms 246
$haystack)
preg_match("/$needle/i", 2 ms 2 ms 2 ms 2 ms 8 ms 240
$haystack)
preg_match("/$needle/S", 2 ms 2 ms 2 ms 2 ms 8 ms 246
$haystack)
ereg($needle, $haystack) 1 ms 2 ms 8 ms 14 ms 25 ms 760

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.

Method Not Found at Found in Found at Summary Index


found the start the the end
middle
$haystack[0] === "n" >0 ms >0 ms >0 ms >0 ms 2 ms 100
strncmp($haystack, $needle, 2 ms 2 ms 2 ms 2 ms 6 ms 394
strlen($needle)) === 0
strncmp($haystack, "needle", 6) 1 ms 1 ms 1 ms 1 ms 3 ms 202
=== 0
strncasecmp($haystack, $needle, 2 ms 2 ms 2 ms 2 ms 6 ms 388
strlen($needle)) === 0
strpos($haystack, $needle) === 0 1 ms 1 ms 1 ms 1 ms 3 ms 207
substr($haystack, 0, 2 ms 2 ms 2 ms 2 ms 7 ms 412
strlen($needle)) === $needle
strcmp(substr($haystack, 0, 3 ms 2 ms 2 ms 2 ms 9 ms 585
strlen($needle)), $needle) === 0
preg_match("/^" . 3 ms 3 ms 3 ms 3 ms 13 ms 830
preg_quote($needle, "/") . "/",
$haystack)

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.

Method Not Found at Found in Found Summary Index


found the start the at the
middle end
$haystack[strlen($haystack) - 1] === 1 ms 1 ms 1 ms 1 ms 4 ms 100
"n"
substr($haystack, strlen($haystack) 2 ms 2 ms 2 ms 2 ms 8 ms 219
- strlen($needle)) === $needle
substr($haystack, -strlen($needle)) 2 ms 2 ms 2 ms 2 ms 6 ms 169
=== $needle
substr($haystack, -1) === "n" 1 ms 1 ms 1 ms 1 ms 4 ms 105
strcmp(substr($haystack, 2 ms 2 ms 2 ms 2 ms 8 ms 213
-strlen($needle)), $needle) === 0
preg_match("/" . preg_quote($needle, 3 ms 4 ms 4 ms 4 ms 14 ms 379
"/") . "$/", $haystack)

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

Method Not Found at Found in Found at Summary Index


found the start the middle the end
str_replace($search, $replace, 2 ms 2 ms 2 ms 2 ms 7 ms 100
$subject)
preg_replace("/$search/", 3 ms 3 ms 3 ms 3 ms 12 ms 178
$replace, $subject)
preg_replace("/$search/S", 3 ms 3 ms 3 ms 3 ms 12 ms 181
$replace, $subject)
ereg_replace($search, $replace, 2 ms 4 ms 9 ms 15 ms 31 ms 463
$subject)
strtr($subject, $array) 11 ms 11 ms 11 ms 11 ms 43 ms 641

My conclusion: Never use the ereg() functions.

Method Not Found Found Found at Summary Index


found at start at end both sides
trim($string, ",") >0 ms >0 ms >0 ms >0 ms 1 ms 100
preg_replace('/^,*|,*$/', "", 6 ms 6 ms 6 ms 6 ms 24 ms 3402
$string)
preg_replace('/^,*|,*$/m', "", 10 ms 10 ms 10 ms 10 ms 41 ms 5856
$string)
preg_replace('/^,+|,+$/', "", >0 ms >0 ms >0 ms >0 ms 2 ms 246
$string)
preg_replace('/^,+|,+$/m', "", >0 ms >0 ms >0 ms >0 ms 2 ms 239
$string)
preg_replace('/^,+/', "", 1 ms 1 ms 1 ms 1 ms 3 ms 414
preg_replace('/,+$/', "", ))

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.

Method Empty Single Multiple Summary Index


string occurrence occurrences
explode(",", $string) 1 ms 1 ms 6 ms 8 ms 100
split(",", $string) 1 ms 1 ms 35 ms 37 ms 439
preg_split("/,/", $string) 1 ms 2 ms 10 ms 14 ms 161
preg_match_all('/[^,]+/', $string, 2 ms 3 ms 17 ms 22 ms 265
$matches)

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

Method Summary Index


for ($i = 0; $i < count($array); $i++) //by reference 35 ms 5224
for ($i = 0; $i < count($array); $i++) //by value 2 ms 283
for ($i = 0, $count = count($array); $i < $count; $i++) 1 ms 130
for ($i = count($array) - 1; $i >= 0; $i--) 1 ms 129
for ($i = count($array) - 1; $i >= 0; --$i) 1 ms 156
$i = count($array); while ($i--) 1 ms 100

My conclusion: count() can be horribly slow when PHP's copy-on-write kicks in. Always precalculate it, if possible.

Method Summary Index


$array[0] 30 ms 104
$array['key'] 29 ms 100

My conclusion: I like associative arrays.

Method Summary Index


implode(" ", $array) 5 ms 112
"$array[0] $array[1] $array[2]" 4 ms 100
$array[0] . " " . $array[1] . " " . $array[2] 4 ms 103
sprintf("%s %s %s", $array[0], $array[1], $array[2]) 9 ms 218
vsprintf("%s %s %s", $array) 10 ms 237

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

Method Summary Index


'contains no dollar signs' 1 ms 102
"contains no dollar signs" 1 ms 101
'$variables $are $not $replaced' 1 ms 102
"\$variables \$are \$not \$replaced" >0 ms 100
"$variables $are $replaced" 7 ms 1413
$variables . ' ' . $are . ' ' . $replaced 8 ms 1588
$variables . " " . $are . " " . $replaced 8 ms 1613

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 Mttig, created in September 2008, updated in February 2016


More PHP experiments