Chinaunix首页 | 论坛 | 博客
  • 博客访问: 921135
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: Python/Ruby

2012-11-23 10:04:51

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  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 empty
MethodUndefinedNullFalseEmpty stringString "0"String "1"Long stringSummaryIndex
if (!$var)2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms4 ms134
if (empty($var))>0 ms1 ms1 ms1 ms1 ms>0 ms>0 ms3 ms100
if ($var == "")2 ms>0 ms>0 ms1 ms1 ms1 ms41 ms46 ms1442
if ("" == $var)2 ms>0 ms>0 ms1 ms1 ms>0 ms>0 ms5 ms163
if ($var === "")2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms3 ms105
if ("" === $var)3 ms>0 ms>0 ms1 ms>0 ms>0 ms>0 ms5 ms151
if (strcmp($var, "") == 0)4 ms2 ms2 ms1 ms1 ms1 ms1 ms14 ms430
if (strcmp("", $var) == 0)4 ms2 ms2 ms2 ms1 ms1 ms1 ms13 ms417
if (strlen($var) == 0)3 ms1 ms1 ms1 ms1 ms1 ms1 ms10 ms308
if (!strlen($var))3 ms1 ms1 ms1 ms1 ms1 ms1 ms9 ms293

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
MethodEqualFirst character not equalLast character not equalSummaryIndex
$a == $b2 ms1 ms2 ms5 ms100
!strcmp($a, $b)4 ms3 ms4 ms11 ms205
strcmp($a, $b) == 04 ms3 ms4 ms11 ms218
strcmp($a, $b) === 04 ms3 ms5 ms12 ms219
strcasecmp($a, $b) === 09 ms3 ms8 ms21 ms392

My conclusion: Use what fits your needs.

Check if a String contains another String
MethodNot foundFound at the startFound in the middleFound at the endSummaryIndex
strstr($haystack, $needle)1 ms1 ms1 ms1 ms5 ms100
strpos($haystack, $needle) !== false1 ms1 ms1 ms2 ms5 ms111
strstr($haystack, $needle) !== false1 ms1 ms1 ms1 ms5 ms109
stristr($haystack, $needle)2 ms2 ms2 ms2 ms8 ms178
preg_match("/$needle/", $haystack)2 ms2 ms2 ms2 ms8 ms167
preg_match("/$needle/i", $haystack)2 ms2 ms2 ms2 ms8 ms175
preg_match("/$needle/S", $haystack)2 ms2 ms2 ms2 ms8 ms166
ereg($needle, $haystack)2 ms2 ms10 ms18 ms32 ms688

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
MethodNot foundFound at the startFound in the middleFound at the endSummaryIndex
strncmp($haystack, $needle, strlen($needle)) === 01 ms1 ms1 ms1 ms6 ms139
strncmp($haystack, "Test", 4) === 01 ms1 ms1 ms1 ms5 ms109
strncasecmp($haystack, $needle, strlen($needle)) === 01 ms1 ms1 ms2 ms6 ms142
strpos($haystack, $needle) === 01 ms1 ms1 ms1 ms4 ms100
substr($haystack, 0, strlen($needle)) === $needle2 ms2 ms2 ms2 ms7 ms176
strcmp(substr($haystack, 0, strlen($needle)), $needle) === 02 ms2 ms3 ms3 ms10 ms246
preg_match("/^" . preg_quote($needle, "/") . "/", $haystack)3 ms3 ms3 ms3 ms13 ms304

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
MethodNot foundFound at the startFound in the middleFound at the endSummaryIndex
substr($haystack, strlen($haystack) - strlen($needle)) === $needle2 ms2 ms2 ms2 ms8 ms117
substr($haystack, -strlen($needle)) === $needle2 ms2 ms2 ms2 ms7 ms100
strcmp(substr($haystack, -strlen($needle)), $needle) === 03 ms3 ms4 ms4 ms14 ms196
preg_match("/" . preg_quote($needle, "/") . "$/", $haystack)6 ms4 ms4 ms4 ms17 ms239

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

Replace a String inside another String
MethodNot foundFound at the startFound in the middleFound at the endSummaryIndex
str_replace($search, $replace, $subject)2 ms2 ms2 ms2 ms8 ms100
preg_replace("/$search/", $replace, $subject)3 ms4 ms4 ms4 ms14 ms183
preg_replace("/$search/S", $replace, $subject)3 ms3 ms5 ms3 ms14 ms190
ereg_replace($search, $replace, $subject)3 ms7 ms12 ms19 ms41 ms545

My conclusion: Never use the ereg…() functions.

Trim Characters from the Beginning and End of a String
MethodNot foundFound at startFound at endFound at both sidesSummaryIndex
trim($string, ",")>0 ms>0 ms>0 ms>0 ms1 ms100
preg_replace('/^,*|,*$/', "", $string)7 ms8 ms7 ms7 ms28 ms3566
preg_replace('/^,*|,*$/m', "", $string)12 ms11 ms11 ms11 ms45 ms5775
preg_replace('/^,+|,+$/', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms243
preg_replace('/^,+|,+$/m', "", $string)>0 ms>0 ms>0 ms>0 ms2 ms237
preg_replace('/^,+/', "", preg_replace('/,+$/', "", …))1 ms1 ms1 ms1 ms3 ms420

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
MethodEmpty stringSingle occurrenceMultiple occurrencesSummaryIndex
explode(",", $string)1 ms1 ms8 ms10 ms100
split(",", $string)1 ms3 ms47 ms52 ms497
preg_split("/,/", $string)2 ms2 ms11 ms15 ms143
preg_match_all('/[^,]+/', $string, $matches)2 ms4 ms18 ms24 ms232

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
MethodSummaryIndex
for ($i = 0; $i < count($array); $i++)43 ms6054
for ($i = 0, $count = count($array); $i < $count; $i++)1 ms141
for ($i = count($array) - 1; $i >= 0; $i--)1 ms158
for ($i = count($array) - 1; $i >= 0; --$i)1 ms155
$i = count($array); while ($i--)1 ms100

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

Get Elements from an Array
MethodSummaryIndex
$array[0]40 ms118
$array['key']34 ms100

My conclusion: I like associative arrays.

Implode an Array
MethodSummaryIndex
implode(" ", $array)5 ms113
"$array[0] $array[1] $array[2]"5 ms101
$array[0] . " " . $array[1] . " " . $array[2]5 ms100
sprintf("%s %s %s", $array[0], $array[1], $array[2])10 ms217
vsprintf("%s %s %s", $array)12 ms253

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

The single vs. double Quotes Myth
MethodSummaryIndex
'contains no dollar signs'1 ms100
"contains no dollar signs"1 ms107
'$variables $are $not $replaced'1 ms102
"\$variables \$are \$not \$replaced"1 ms107
"$variables $are $replaced"7 ms1192
$variables . ' ' . $are . ' ' . $replaced9 ms1445
$variables . " " . $are . " " . $replaced8 ms1402

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.

阅读(840) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~