在 PHP 中跟踪脚本执行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/535020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tracking the script execution time in PHP
提问by twk
PHP must track the amount of CPU time a particular script has used in order to enforce the max_execution_time limit.
PHP 必须跟踪特定脚本使用的 CPU 时间量以强制执行 max_execution_time 限制。
Is there a way to get access to this inside of the script? I'd like to include some logging with my tests about how much CPU was burnt in the actual PHP (the time is not incremented when the script is sitting and waiting for the database).
有没有办法在脚本中访问它?我想在我的测试中包含一些关于实际 PHP 中消耗了多少 CPU 的日志(当脚本处于等待数据库时,时间不会增加)。
I am using a Linux box.
我正在使用 Linux 机器。
采纳答案by phihag
On unixoid systems (and in php 7+ on Windows as well), you can use getrusage, like:
在 unixoid 系统上(以及在 Windows 上的 php 7+ 中),您可以使用getrusage,例如:
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";
Note that you don't need to calculate a difference if you are spawning a php instance for every test.
请注意,如果您为每个测试生成一个 php 实例,则不需要计算差异。
回答by talal7860
If all you need is the wall-clock time, rather than the CPU execution time, then it is simple to calculate:
如果您只需要挂钟时间,而不是 CPU 执行时间,那么计算起来很简单:
//place this before any script you want to calculate time
$time_start = microtime(true);
//sample script
for($i=0; $i<1000; $i++){
//do anything
}
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10)
Note that this will include time that PHP is sat waiting for external resources such as disks or databases, which is not used for max_execution_time.
请注意,这将包括 PHP 等待外部资源(例如磁盘或数据库)的时间,这些时间不用于 max_execution_time。
回答by C. Lee
Shorter version of talal7860's answer
talal7860 答案的较短版本
<?php
// At start of script
$time_start = microtime(true);
// Anywhere else in the script
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
As pointed out, this is 'wallclock time' not 'cpu time'
正如所指出的,这是“挂钟时间”而不是“CPU时间”
回答by joan16v
The easiest way:
最简单的方法:
<?php
$time1 = microtime(true);
//script code
//...
$time2 = microtime(true);
echo 'script execution time: ' . ($time2 - $time1); //value in seconds
回答by Joyal
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
?>
回答by Hamid Tavakoli
I created an ExecutionTime class out of phihag answer that you can use out of box:
我从 phihag 答案中创建了一个 ExecutionTime 类,您可以开箱即用:
class ExecutionTime
{
private $startTime;
private $endTime;
public function start(){
$this->startTime = getrusage();
}
public function end(){
$this->endTime = getrusage();
}
private function runTime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
public function __toString(){
return "This process used " . $this->runTime($this->endTime, $this->startTime, "utime") .
" ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") .
" ms in system calls\n";
}
}
usage:
用法:
$executionTime = new ExecutionTime();
$executionTime->start();
// code
$executionTime->end();
echo $executionTime;
Note: In PHP 5, the getrusage function only works in Unix-oid systems. Since PHP 7, it also works on Windows.
注意:在 PHP 5 中,getrusage 函数只适用于 Unix-oid 系统。从 PHP 7 开始,它也适用于 Windows。
回答by lencho patasplanas
Gringod at developerfusion.com gives this good answer:
developerfusion.com 上的 Gringod 给出了这个很好的答案:
<!-- put this at the top of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
;?>
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
;?>
From (http://www.developerfusion.com/code/2058/determine-execution-time-in-php/)
来自(http://www.developerfusion.com/code/2058/determine-execution-time-in-php/)
回答by Sinan Eldem
It is going to be prettier if you format the seconds output like:
如果你格式化秒输出,它会更漂亮:
echo "Process took ". number_format(microtime(true) - $start, 2). " seconds.";
will print
将打印
Process took 6.45 seconds.
This is much better than
这比
Process took 6.4518549156189 seconds.
回答by danieltalsky
The cheapest and dirtiest way to do it is simply make microtime()calls at places in your code you want to benchmark. Do it right before and right after database queries and it's simple to remove those durations from the rest of your script execution time.
最便宜和最脏的方法就是microtime()在代码中要进行基准测试的地方进行调用。在数据库查询之前和之后立即执行此操作,从脚本执行时间的其余部分中删除这些持续时间很简单。
A hint: your PHP execution time is rarely going to be the thing that makes your script timeout. If a script times out it's almost always going to be a call to an external resource.
提示:您的 PHP 执行时间很少会导致您的脚本超时。如果脚本超时,它几乎总是会调用外部资源。
PHP microtime documentation: http://us.php.net/microtime
PHP微时间文档:http: //us.php.net/microtime
回答by Stewart Robinson
I think you should look at xdebug. The profiling options will give you a head start toward knowing many process related items.
我认为你应该看看 xdebug。分析选项将使您在了解许多与流程相关的项目方面处于领先地位。

