PHP中的异常处理对程序执行效率的影响有哪些?
前天看了篇文章,是关于C#的异常处理的。文中提出C#中的异常处理会对程序的执行效率产生很大影响,对于相同的一个算法,使用异常处理和使用Return返回错误的程序的时间差可以达到6s : 1s。
我用php5做了个测试,结果发现php5的异常处理也会对程序运行效率产生较大的影响。
平台:winXP sp2 / apache 2.0 / php5.1
程序分别如下:
使用了exception处理
<?php
functiongetmicrotime(){
list($usec,$sec)=explode("",microtime());
return((float)$usec+(float)$sec);
}
$time_start=getmicrotime();
classA{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
thrownewexception("ClassAdisplay()called.<br/>");
}
}
classBextendsA
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassBdisplay()called.<br/>");
}
}
}
classCextendsB
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassCdisplay()called.<br/>");
}
}
}
classDextendsC
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassDdisplay()called.<br/>");
}
}
}
try{
$ss=newd;
}catch(exception$e)
{
echo$e->getMessage();
}
$time_end=getmicrotime();
$time=$time_end-$time_start;
echo"Didnothingin$timeseconds";
?>
functiongetmicrotime(){
list($usec,$sec)=explode("",microtime());
return((float)$usec+(float)$sec);
}
$time_start=getmicrotime();
classA{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
thrownewexception("ClassAdisplay()called.<br/>");
}
}
classBextendsA
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassBdisplay()called.<br/>");
}
}
}
classCextendsB
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassCdisplay()called.<br/>");
}
}
}
classDextendsC
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
try{
parent::display();
}catch(exception$e){
thrownewexception($e->getMessage()."ClassDdisplay()called.<br/>");
}
}
}
try{
$ss=newd;
}catch(exception$e)
{
echo$e->getMessage();
}
$time_end=getmicrotime();
$time=$time_end-$time_start;
echo"Didnothingin$timeseconds";
?>
使用return 返回错误
<?php
functiongetmicrotime(){
list($usec,$sec)=explode("",microtime());
return((float)$usec+(float)$sec);
}
$time_start=getmicrotime();
classA{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
echo"ClassAdisplay()called.<br/>";
returnfalse;
}
}
classBextendsA
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassBdisplay()called.<br/>";
returnfalse;
}
}
}
classCextendsB
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassCdisplay()called.<br/>";
returnfalse;
}
}
}
classDextendsC
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassDdisplay()called.<br/>";
returnfalse;
}
}
}
$ss=newd;
$time_end=getmicrotime();
$time=$time_end-$time_start;
echo"Didnothingin$timeseconds";
?>
functiongetmicrotime(){
list($usec,$sec)=explode("",microtime());
return((float)$usec+(float)$sec);
}
$time_start=getmicrotime();
classA{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
echo"ClassAdisplay()called.<br/>";
returnfalse;
}
}
classBextendsA
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassBdisplay()called.<br/>";
returnfalse;
}
}
}
classCextendsB
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassCdisplay()called.<br/>";
returnfalse;
}
}
}
classDextendsC
{
publicfunction__construct()
{
$this->display();
}
publicfunctiondisplay()
{
if(!parent::display()){
echo"ClassDdisplay()called.<br/>";
returnfalse;
}
}
}
$ss=newd;
$time_end=getmicrotime();
$time=$time_end-$time_start;
echo"Didnothingin$timeseconds";
?>
测试结果如下:
exception处理 | return处理 | |
第一次 | 0.00075888633728s | 0.000170946121216s |
第二次 | 0.000414133071899s | 0.0001380443573s |
第三次 | 0.000410079956055s | 0.000159978866577s |
第四次 | 0.000416040420532s | 0.000160932540894s |
第五次 | 0.00037693977356s | 0.000144958496094s |
第六次 | 0.000380039215088s | 0.000140905380249s |
第七次 | 0.000383853912354s | 0.000147819519043s |
本文地址:http://www.45fan.com/dnjc/69637.html