首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何测量用PHP编写的代码的速度?

如何测量用PHP编写的代码的速度?
EN

Stack Overflow用户
提问于 2009-07-29 13:20:46
回答 6查看 95.6K关注 0票数 125

我如何才能说出多个(都做同样的工作)中的哪一个类执行得更快?有没有软件来衡量这一点?

EN

回答 6

Stack Overflow用户

发布于 2009-07-29 13:30:15

为了更快,我这样做(用PHP):

代码语言:javascript
复制
$startTime = microtime(true);
doTask(); // whatever you want to time
echo "Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";

您也可以使用像http://xdebug.org/这样的分析器。

票数 48
EN

Stack Overflow用户

发布于 2015-07-30 22:02:46

我做了一个简单的计时类,可能对某些人有用:

代码语言:javascript
复制
class TimingHelper {

    private $start;

    public function __construct() {
        $this->start = microtime(true);
    }

    public function start() {
        $this->start = microtime(true);
    }

    public function segs() {
        return microtime(true) - $this->start;
    }

    public function time() {
        $segs = $this->segs();
        $days = floor($segs / 86400);
        $segs -= $days * 86400;
        $hours = floor($segs / 3600);
        $segs -= $hours * 3600;
        $mins = floor($segs / 60);
        $segs -= $mins * 60;
        $microsegs = ($segs - floor($segs)) * 1000;
        $segs = floor($segs);

        return 
            (empty($days) ? "" : $days . "d ") . 
            (empty($hours) ? "" : $hours . "h ") . 
            (empty($mins) ? "" : $mins . "m ") . 
            $segs . "s " .
            $microsegs . "ms";
    }

}

使用:

代码语言:javascript
复制
$th = new TimingHelper();
<..code being mesured..>
echo $th->time();
$th->start(); // if it's the case
<..code being mesured..>
echo $th->time();

// result: 4d 17h 34m 57s 0.00095367431640625ms 
票数 11
EN

Stack Overflow用户

发布于 2009-07-30 00:16:56

我最近一直在用XHProf,http://pecl.php.net/package/xhprof。它最初是由Facebook开发的,它有一个不错的web界面。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1200214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档