文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>写了测试脚本块运行花的时间的类

写了测试脚本块运行花的时间的类

时间:2007-07-31  来源:lib

?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 . '
';
    }
}
/* 调用 */
/*
RunedTime::begin();
RunedTime::end();
print RunedTime::get() . '
';
RunedTime::begin();
RunedTime::end();
RunedTime::show() . '
';
RunedTime::begin();
RunedTime::end();
print RunedTime::get() . '
';
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; $i100000; $i++) $tmp += Test::con;
RunedTime::end();
RunedTime::begin();
$tmp = 0;
for($i=0; $i100000; $i++) $tmp += $cls->vars;
RunedTime::end();
RunedTime::begin();
$tmp = 0;
for($i=0; $i100000; $i++) $tmp +=Test::$vars2;
RunedTime::end();
var_dump( RunedTime::$runedTimeList);


相关阅读 更多 +
排行榜 更多 +
翌日波奇狗的历险记手机版下载

翌日波奇狗的历险记手机版下载

休闲益智 下载
怪兽远征安卓版下载

怪兽远征安卓版下载

角色扮演 下载
谷歌卫星地图免费版下载

谷歌卫星地图免费版下载

生活实用 下载