Home  >  Article  >  Backend Development  >  看过google的搜索吗?搜索共花了多少时间?这里有一个类可以统计_PHP

看过google的搜索吗?搜索共花了多少时间?这里有一个类可以统计_PHP

WBOY
WBOYOriginal
2016-06-01 12:28:46901browse

Google


// class PHP_timer开始
class PHP_timer {

    // 用来收集脚本执行过程中的信息
    var $points = array();
    
    // 在脚本的开始处调用这个函数
    function start() {
        // 请看后面的addmarker函数
        $this->addmarker("Start");
    }
    // end function start()

    // 在脚本的结束处调用这个函数
    function stop() {
        // 请看后面的addmarker函数
        $this->addmarker("Stop");
    }
    // end function stop()

    // 这个函数用来在脚本执行时增加一个标记
    // 需要一个用来描述的名字
    function addmarker($name) {
        // 调用 jointime() 函数并将microtime() 的返回值传递过去
        $markertime = $this->jointime(microtime());
        // $ae 得到当前数组的大小,也就是当前的插入位置
        // currently in the $points array
        $ae = count($this->points);
        // 在数组中存储timestamp 和说明
        $this->points[$ae][0] = $markertime;
        $this->points[$ae][1] = $name;
    }
    // end function addmarker()

    // 这个函数会处理从microtime() 返回的字串
    function jointime($mtime) {
        // 分解字串
        $timeparts = explode(" ",$mtime);
        // 连接两个字串,并去除小数部分的0
        $finaltime = $timeparts[1].substr($timeparts[0],1);
        // 返回连接后的字串
        return $finaltime;
    }
    // end function jointime()
    
    // 这个函数简单的显示从开始到结束所需要的时间
    function showtime() {
        echo bcsub($this->points[count($this->points)-1][0],$this->points[0][0],6);
    }
    // end function showtime()
    
    // 这个函数显示所有的在脚本运行过程中收集到的信息
    function debug() {
        echo "Script execution debug information:";
        echo "\n";
        // 这个表格有3列 Marker name, Timestamp, difference
        echo "\n";
        // 第一行是没有运行时间差的
        echo "\n";
        echo "";
        echo "";
        echo "\n";
        echo "\n";
        // 从数组的元素1开始循环显示,0已经在第一行显示过了
        for ($i = 1; $i points);$i++) {
            echo "\n";
            echo "";
            echo "";
            echo "";
            echo "\n";
        }
        echo "
Marker Time Diff
".$this->points[0][1]."".$this->points[0][0]."-
".$this->points[$i][1]."".$this->points[$i][0]."";
            // 显示上下两行的时间差
            echo bcsub($this->points[$i][0],$this->points[$i-1][0],6);
            echo "
";
    }
    // end function debug()
}
// end class PHP_timer

?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn