<?php /** * 测试脚本块运行花的时间 * * @author 流水孟春 121169238#qq.com * * 有很多框架都提供测试时间的支持,我也回头耍了个小儿科的弄个很简单的一个 * 第一次写使用这么密集的 self 的类 ^_^ * * 在开始计时处插入 RunedTime::begin(); * 在结束计时处插入 RunedTime::end(); * 用RunedTime::get(); 获取代码块运行时间 * */ class RunedTime { private static $_begin = 0; // 开始计时
private static $_end = 0; // 计时结束
private static $_runedTime = 0; // 执行时间
private static $_times = 0; // 第几次测试时间
public static $runedTimeList = array(); // 保存第几次测试和测试结果
private static function _timeNow() { return time()+(float) microtime(); }
/** * 开始计时 * */ public static function begin() { self::$_begin = self::_timeNow(); }
/** * 计时结束 * */ public static function end() { self::$_end = self::_timeNow(); self::$_runedTime = self::$_end - self::$_begin; self::$runedTimeList[self::$_times++] = self::$_runedTime; }
/** * 运行时间 * */ public static function get() { return self::$_runedTime; }
/** * 全部时间集合 * */ public static function show() { print self::$_runedTime . '<br>'; } }
/* 调用 */ /* RunedTime::begin(); RunedTime::end(); print RunedTime::get() . '<br>';
RunedTime::begin(); RunedTime::end(); RunedTime::show() . '<br>';
RunedTime::begin(); RunedTime::end(); print RunedTime::get() . '<br>';
var_dump( RunedTime::$runedTimeList); */
class Test { const con = 1; public $vars = 1; public static $vars2 = 1; }
$cls = new Test();
RunedTime::begin(); $tmp = 0; for($i=0; $i<100000; $i++) $tmp += Test::con; RunedTime::end();
RunedTime::begin(); $tmp = 0; for($i=0; $i<100000; $i++) $tmp += $cls->vars; RunedTime::end();
RunedTime::begin(); $tmp = 0; for($i=0; $i<100000; $i++) $tmp +=Test::$vars2; RunedTime::end();
var_dump( RunedTime::$runedTimeList);
|
|