twk*_*twk 269 php performance
PHP必须跟踪特定脚本用于强制执行max_execution_time限制的CPU时间量.
有没有办法在脚本内部访问这个?我想在我的测试中包含一些日志,记录实际PHP中有多少CPU被烧毁(当脚本处于等待数据库时,时间不会增加).
我正在使用Linux机器.
小智 481
如果您只需要挂钟时间而不是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)
Run Code Online (Sandbox Code Playgroud)
请注意,这将包括PHP等待外部资源(如磁盘或数据库)的时间,这些资源不用于max_execution_time.
phi*_*hag 214
在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";
Run Code Online (Sandbox Code Playgroud)
请注意,如果要为每个测试生成一个php实例,则无需计算差异.
C. *_*Lee 103
更短版本的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);
Run Code Online (Sandbox Code Playgroud)
正如所指出的,这是'挂钟时间'而不是'cpu时间'
joa*_*16v 70
最简单的方法:
<?php
$time1 = microtime(true);
//script code
//...
$time2 = microtime(true);
echo 'script execution time: ' . ($time2 - $time1); //value in seconds
Run Code Online (Sandbox Code Playgroud)
Joy*_*yal 33
<?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";
?>
Run Code Online (Sandbox Code Playgroud)
Ham*_*oli 25
我创建了一个可以在开箱即用的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";
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
$executionTime = new ExecutionTime();
$executionTime->start();
// code
$executionTime->end();
echo $executionTime;
Run Code Online (Sandbox Code Playgroud)
注意:在PHP 5中,getrusage函数仅适用于Unix-oid系统.从PHP 7开始,它也适用于Windows.
小智 12
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";
;?>
Run Code Online (Sandbox Code Playgroud)
来自(http://www.developerfusion.com/code/2058/determine-execution-time-in-php/)
Ari*_*ris 11
要显示分钟和秒,您可以使用:
$startTime = microtime(true);
$endTime = microtime(true);
$diff = round($endTime - $startTime);
$minutes = floor($diff / 60); //only minutes
$seconds = $diff % 60;//remaining seconds, using modulo operator
echo "script execution time: minutes:$minutes, seconds:$seconds"; //value in seconds
Run Code Online (Sandbox Code Playgroud)
最便宜和最脏的方法就是microtime()在您想要进行基准测试的代码中的地方拨打电话.在数据库查询之前和之后立即执行,并且很容易从脚本执行的其余时间中删除这些持续时间.
提示:您的PHP执行时间很少会使您的脚本超时.如果脚本超时,它几乎总是调用外部资源.
PHP microtime文档:http: //us.php.net/microtime
如果您将秒输出格式化为更漂亮,那将会更漂亮:
echo "Process took ". number_format(microtime(true) - $start, 2). " seconds.";
Run Code Online (Sandbox Code Playgroud)
将打印
Process took 6.45 seconds.
Run Code Online (Sandbox Code Playgroud)
这要好得多
Process took 6.4518549156189 seconds.
Run Code Online (Sandbox Code Playgroud)
当 PHP 中有闭包功能时,我们为什么不从中受益呢?
function startTime(){
$startTime = microtime(true);
return function () use ($startTime){
return microtime(true) - $startTime;
};
}
Run Code Online (Sandbox Code Playgroud)
现在借助上述函数,我们可以像这样跟踪时间
$stopTime = startTime();
//some code block or line
$elapsedTime = $stopTime();
Run Code Online (Sandbox Code Playgroud)
每次调用startTime函数都会启动一个单独的时间跟踪器。因此,您可以根据需要发起任意数量的操作,也可以在任意位置停止它们。
$_SERVER['REQUEST_TIME']也检查一下。IE
...
// your codes running
...
echo (time() - $_SERVER['REQUEST_TIME']);
Run Code Online (Sandbox Code Playgroud)