Timer类
经常需要测试PHP代码执行时间,决定代码是否需要优化,在网上找了个Timer类,与大家分享!
PHP代码
<?php class Timer{ private $start = 0; private $end = 0; private function now(){ list($usec,$sec) = explode(' ',microtime()); return ((float)$usec + (float)$sec); } public function start(){ $this->start = $this->now(); } public function end(){ $this->end = $this->now(); } public function getTime(){ return (float)($this->end - $this->start); } public function printTime(){ printf("Program run use time:%fs/n",$this->getTime()); } }
如何使用?
$obj = new Timer(); $obj->start(); ...代码 $obj->end(); $obj->printTime();
浙公网安备 33010602011771号