1.
<?php
date_default_timezone_set(\'PRC\');
header(\'Content-Type:text/html;charset=utf8\');
/**
* 用来测试一个函数或一段代码的执行时间
* @author liangdaguang
*/
class BaseRunTime
{
private static $_start_time; //开始时间
private static $_end_time; //结束时间
private static $_total_time; //总的时间
public function main()
{
self::start_time();
self::end_time();
return self::time_spend();
}
private function start_time()
{
list($usec,$sec) = explode(\' \', microtime());
self:: $_start_time = $sec+$usec;
}
private function end_time()
{
list($usec, $sec) = explode(\' \', microtime());
self::$_end_time = $usec + $sec;
}
private function time_spend()
{
if(isset(self::$_start_time) && !is_null(self::$_start_time) && isset(self::$_end_time) && !is_null(self::$_end_time))
self::$_total_time = \'此次页面共执行了:\'.(self::$_end_time - self::$_start_time);
else
self::$_total_time = \'请设置开时间或结束时间\';
return self::$_total_time;
}
}
$result = BaseRunTime::main();
echo $result;