免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1845 | 回复: 0
打印 上一主题 下一主题

测试PHP5 的类的setter的性能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-25 01:05 |只看该作者 |倒序浏览
写了个脚本来测试PHP5 的类的setter(http://www.php.net/manual/en/language.oop5.overloading.php)的性能,跑了100000次,
在我的windows XP破机上速度是 0.68417501449585 -- 0.2145471572876
在linux上测试0.30176782608032 -- 0.096343040466309
所有,在设计类的时候有必要用setter,getter的尽管放心性能上的问题
?php
$xtimes = 100000;
$att = '';
for ($i=0;$i$xtimes;$i++) {
    $att .= "\r\n\tpublic \$var$i  = $i;";
}
$s = END
?php\r\n
class MC {
$att
\r\n
}\r\n
END;
$s .= '
$xn = ' . $xtimes . ';
class Setter {
   public $n;
   private $x = array();
   private function __get($nm) {
       if (isset($this->x[$nm])) {
           $r = $this->x[$nm];
           return $r = $this->x[$nm];;
       } else {
           return false;
       }
   }
   private function __set($nm, $val) {     
       if (!isset($this->x[$nm])) {
           $this->x[$nm] = $val;           
       }
   }
   private function __isset($nm) {
       return isset($this->x[$nm]);
   }
   private function __unset($nm) {      
       unset($this->x[$nm]);
   }
}
$foo = new Setter();
$star = microtime(1);
for ($i=0; $i$var = $i;
}
$out = microtime(1) - $star;
print "\n";
$foo2 = new MC();
$star = microtime(1);
for ($i=0; $i$var = $i;
}
$out .= " -- " . (microtime(1) - $star);
print $out . " (this file is save at " . __FILE__ . \')\';
';
file_put_contents('testxxx.php', $s);
require 'estxxx.php';
unlink('testxxx.php'); // 把这行注释掉就可以看生成的文件对不对
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/27731/showart_1334827.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP