登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

[转]PHP Speed Freaks  

2008-05-28 22:12:18|  分类: Web |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

PHP Speed Freaks

Posted April 23rd, 2007 by Mgccl

All pure statements, don't believe me? try yourself.
$i++; is slower than ++$i;
$i += 1; is slower than++$i;
rand($min,$max); is slower thanmt_rand($min,$max);

$i = 0;  while($array[$i]){   ++$i;  }

is slower than

$i = 0;  $count = count($array);  while($i < $count){   ++$i;  }

print 'anything';
is slower than
echo 'anything';

echo 'anything (pure HTML, in php tags)';
is slower than
anything (pure HTML, not in php tags)

include('output/contain_no_php');
is slower than
readfile('output/contain_no_php');
proof.

mysql_fetch_array($result);
is not as smart as
mysql_fetch_assoc($result);

echo "$variable string";
is slower than
echo $variable.'string';

echo $variable.'string';
is slower than
echo $variable,'string';

A_CONSTANT;
$a_variable;
I don't know which one is faster... different test have different results

constant('A_CONSTANT');
is slower than
A_CONSTANT;

pi();
is slower than
M_PI;

if($number<0){   $number *= -1;  }

is slower than
abs($number);

pow($x , $exp) + pow($x , $exp - 1);  $a*$b+$a*$c+$a*$d;

is slower than

($x + 1) * pow($x , $exp - 1);  $a*($b+$c+$d);

Expanded form are usually slower than factorized form.

if($a == $b)
is slower than
if($a === $b)

pow($x, 0.5);
is slower than
sqrt($x);

1/2;
is slower than
0.5;

function some_function($var){   global $newvar;  }

is slower than

function some_function($var){   return $newvar;  }

file($filename);
is not as smart as

$fp=fopen($filename,wb);  flock($fp,LOCK_SH);  $filedata=fread($fp,filesize($filename));  fclose($fp);

sha1('anystring');
is slower than
md5('anystring');

array_rand($array);
is slower than

$count = count($array);  mt_rand(0, $count);

if($a == 30);
is less smart than
if(30 == $a);

if($i != 0);
is slower than
if($i);

$_SERVER["PHP_SELF"];
is slower than
__FILE__;
2nd one is proven faster, here is the article.

$a = 3*4;
is slower than
$a = 3 << 2;

while($i < count($array)){  ++$i;  }

is slower than

$count = count($array);  while($i < $count){  ++$i;  }

$array[string];
is slower than
$array['string'];

preg_match("![0-9]+!", $foo);
is slower than
ctype_digit($foo);
This is from Ilia Alshanetsky's 12 PHP Optimization Tricks

$a = TRUE;
is slower than
$a = true;

for, while loops is slower than do-while loops

do-while loop is the fastest loop ever. For VS While loop benchmark. While VS Do-while in here

I suggest you read Ilia Alshanetsky's zend performance slides and PHP Presents on Performance if you want more optimization knowledge.

  评论这张
 
阅读(861)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018