色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php獲取目標函數(shù)執(zhí)行時間示例

寫了一個類用來測試目標函數(shù)的執(zhí)行時間。以下是類的定義代碼:

復制代碼 代碼如下:
<?php
/**
 * class EfficiencyTester
 * 效率測試器,測試函數(shù)的運行時間
 * @version 1.0 2013.04.13
 * @author Kross
 */
class EfficiencyTester {   
    /**
     * var $testTimes
     * 測試的次數(shù)
     */
    private $testTimes = 1000;

    /**
     * function getTime()
     * 根據(jù)時間模式,獲取時間戳
     * @param $timeModel 時間模式,默認:微秒
     * @return int 時間戳
     */
    private function getTime($timeModel = 'MS') {
        if ($timeModel == 'MS') {
            return microtime();
        } else if ($timeModel == 'S') {
            return time();
        } else {
            return microtime();
        }
    }
    /**
     * function testOnce()
     * 測試目標函數(shù)一次,返回運行時間
     * @param $functionName 目標函數(shù)名
     * @param $timeModel 時間模式,默認:微秒
     * @return double 目標函數(shù)運行一次的時間(很隨機)
     */
    public function testOnce($functionName, $timeModel = 'MS') {       
        $startMicroTime = $this->getTime($timeModel);
        $functionName();
        $endMicroTime = $this->getTime($timeModel);

        $costMicroTime = $endMicroTime - $startMicroTime;

        return $costMicroTime;
    }
    /**
    * function test()
    * 測試目標函數(shù)多次,返回運行時間(平均值)
    * @param $functionName 目標函數(shù)名
    * @param $timeModel 時間模式,默認:微秒
    * @return double 目標函數(shù)運行的時間
    */
    public function test($functionName, $timeModel = 'MS') {
        $totalMicroTimes = 0;
        for ($i = 1; $i <= $this->testTimes; $i++) {
            $totalMicroTimes += $this->testOnce($functionName);
        }
        return $totalMicroTimes / $this->testTimes;
    }
}
?>

以下是類的測試代碼:

復制代碼 代碼如下:
<?php
require_once('../class/EfficiencyTester.class.php');
$e = new EfficiencyTester();
echo $e->test('rand');
?>

一開始我是直接使用 microtime() 獲取時間的,后來考慮到如果想獲得單位是秒的運行時間,這樣寫就不夠多態(tài)了,然后我就寫了一個getTime() 的函數(shù)來獲取不同單位的時間戳,不過這樣,貌似目標函數(shù)的運行時間變長了,可能是因為 getTime() 函數(shù)中的判斷占用了一部分時間。

php技術(shù)php獲取目標函數(shù)執(zhí)行時間示例,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 商城县| 错那县| 乌拉特前旗| 武陟县| 余江县| 秀山| 湄潭县| 方城县| 新泰市| 江门市| 根河市| 水富县| 科尔| 凤台县| 涞源县| 汝州市| 韶关市| 壤塘县| 玉溪市| 营山县| 申扎县| 弥渡县| 威远县| 社旗县| 定远县| 威海市| 嘉峪关市| 万荣县| 宜良县| 南丰县| 米泉市| 景德镇市| 婺源县| 从化市| 兴隆县| 洞头县| 吉林省| 开封市| 大余县| 怀宁县| 平定县|