首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >精确测量php脚本执行时间的方法

精确测量php脚本执行时间的方法
EN

Stack Overflow用户
提问于 2011-06-06 05:25:42
回答 13查看 302.4K关注 0票数 314

我想知道执行一个PHP for循环需要多少毫秒。

我知道泛型算法的结构,但不知道如何在PHP中实现它:

代码语言:javascript
复制
Begin
init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;
End
EN

回答 13

Stack Overflow用户

发布于 2013-08-20 21:58:14

您可以通过以下方式使用microtime(true)

将这段代码放在php文件的开头:

代码语言:javascript
复制
//place this before any script you want to calculate time
$time_start = microtime(true);

//你的脚本代码在这里

代码语言:javascript
复制
// do something

将此代码放在php文件的末尾:

代码语言:javascript
复制
// Display Script End time
$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes other wise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';

它将以minutes格式输出结果。

票数 90
EN

Stack Overflow用户

发布于 2013-03-18 23:46:28

创建文件loadtime.php

代码语言:javascript
复制
<?php
class loadTime{
    private $time_start     =   0;
    private $time_end       =   0;
    private $time           =   0;
    public function __construct(){
        $this->time_start= microtime(true);
    }
    public function __destruct(){
        $this->time_end = microtime(true);
        $this->time = $this->time_end - $this->time_start;
        echo "Loaded in $this->time seconds\n";
    }
}

而不是乞求你的脚本,在<?php之后写include 'loadtime.php'; $loadtime=new loadTime();

当页面在最后加载时,会写上“在x秒内加载”。

票数 30
EN

Stack Overflow用户

发布于 2011-06-06 05:29:16

代码语言:javascript
复制
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
    // do something
}
$total = microtime(true) - $start;
echo $total;
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6245971

复制
相关文章

相似问题

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