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();  

 

 

 

 

posted @ 2010-07-13 09:40  Iwillbback  阅读(84)  评论(0)    收藏  举报