Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP > PHP中的异常处理对程序执行效率的影响
【标  题】:PHP中的异常处理对程序执行效率的影响
【关键字】:PHP
【来  源】:http://blog.csdn.net/surreyKong/archive/2006/10/13/1333717.aspx

PHP中的异常处理对程序执行效率的影响

Your Ad Here 前天看了篇文章,是关于C#的异常处理的。文中提出C#中的异常处理会对程序的执行效率产生很大影响,对于相同的一个算法,使用异常处理和使用Return返回错误的程序的时间差可以达到6s : 1s。
    我用php5做了个测试,结果发现php5的异常处理也会对程序运行效率产生较大的影响。
 
平台:winXP sp2 / apache 2.0 / php5.1
 
程序分别如下:
 
使用了exception处理
<?php
function getmicrotime(){
   
list($usec, $sec= explode(" ",microtime());
   
return ((float)$usec + (float)$sec);
   }
$time_start = getmicrotime();
class A {
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
throw new exception("Class A display() called.<br />");
 }
}
class B extends A
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
try {
   parent
::display();
  } 
catch (exception $e) {
   
throw new exception($e->getMessage()."Class B display() called.<br />");
  }
 }
}
class C extends B
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
try {
   parent
::display();
  } 
catch (exception $e) {
   
throw new exception($e->getMessage()."Class C display() called.<br />");
  }
 }
}
class D extends C
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
try {
   parent
::display();
  } 
catch (exception $e) {
   
throw new exception($e->getMessage()."Class D display() called.<br />");
  }
 }
}
try {
 
$ss = new d;
catch(exception $e)
{
 
echo $e->getMessage();
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds";
?>

使用return 返回错误

 

<?php
function getmicrotime(){
   
list($usec, $sec= explode(" ",microtime());
   
return ((float)$usec + (float)$sec);
   }
$time_start = getmicrotime();
class A {
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
echo  "Class A display() called.<br />";
  
return false;
 }
}
class B extends A
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
if(!parent::display()) {
   
echo  "Class B display() called.<br />";
   
return false;
  }
 }
}
class C extends B
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
if(!parent::display()) {
   
echo  "Class C display() called.<br />";
   
return false;
  }
 }
}
class D extends C
{
 
public function __construct()
 {
  
$this->display();
 }
 
public function display()
 {
  
if(!parent::display()) {
   
echo  "Class D display() called.<br />";
   
return false;
  }
 }
}
 
$ss = new d;
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds";
?>

 

测试结果如下:

   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
 
掌握 Ajax:Ajax 简介:【上一篇】
如何将PHP的数组转化成对象:【下一篇】
【相关文章】
  • PHP+MySQL分页显示示例分析
  • 利用PHP和AJAX创建RSS聚合器
  • Developer's Dilemma: PHP or Perl?
  • 五个常见 PHP 数据库问题
  • php5中时间区域的设置
  • 明小子的php注入工具
  • Apache + Php + Mysql (windows)
  • 菜鸟学PHP之Smarty入门
  • Apache 2.2, MySQL 5 and PHP 5 in FreeBSD
  • PHP服务器安装
  • 【随机文章】
  • J2ME程序设计的几个原则
  • 用 iframe 解决下拉框与层之冲突
  • 流媒体播放器暴风影音使用技巧十则
  • 腾讯 QQ2004介绍
  • Flash MX实战精选:动画片制作
  • .net学习资料集合
  • [候捷]Java反射机制
  • smtpclass类发邮件的应用
  • Quota
  • 3900/5600端口镜象配置
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.