博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

流水孟春

以前使用的博客 webvista.cublog.cn
lib.cublog.cn
写了测试脚本块运行花的时间的类
<?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);

发表于: 2007-07-31,修改于: 2007-08-03 11:55,已浏览696次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:0.01374