一个计算脚本运行时间的小方法

// 设置默认时区
date_default_timezone_set('PRC');
// 声明一个计算脚本运行时间的类
class Timer {
    static private $startTime = 0; // 开始时间 微妙保存
    static private $endTime = 0;    // 结束时间 微妙保存
 
    // 在脚本开始处调用获取脚本运行开始时间的微秒值
    static function start() {
        self::$startTime = microtime(true);
    }   
 
    // 在脚本结束处调用
    static function end() {
        self::$endTime = microtime(true);
    }   
 
    // 返回同一个脚本中两次获取的时间差
    static function spent() {
        return round(self::$endTime - self::$startTime, 4); 
    }   
}
 
Timer::start();
usleep(1000); // 延时1毫秒
Timer::end();
echo Timer::spent();
posted @ 2014-11-05 18:17  pemako  阅读(206)  评论(0)    收藏  举报