我想知道执行一个PHP for循环需要多少毫秒。
我知道一个泛型算法的结构,但不知道如何在PHP中实现它:
Begin
init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;
End
我想知道执行一个PHP for循环需要多少毫秒。
我知道一个泛型算法的结构,但不知道如何在PHP中实现它:
Begin
init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;
End
当前回答
你可以通过以下方式使用微时间(true):
把这个放在你的php文件的开头:
//place this before any script you want to calculate time
$time_start = microtime(true);
//你的脚本代码在这里
// do something
把这个放在你的php文件的末尾:
// 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';
它将在几分钟内输出结果。
其他回答
您可以用一个函数找到以秒为单位的执行时间。
// ampersand is important thing here
function microSec( & $ms ) {
if (\floatval( $ms ) == 0) {
$ms = microtime( true );
}
else {
$originalMs = $ms;
$ms = 0;
return microtime( true ) - $originalMs;
}
}
// you don't have to define $ms variable. just function needs
// it to calculate the difference.
microSec($ms);
sleep(10);
echo microSec($ms) . " seconds"; // 10 seconds
for( $i = 0; $i < 10; $i++) {
// you can use same variable everytime without assign a value
microSec($ms);
sleep(1);
echo microSec($ms) . " seconds"; // 1 second
}
for( $i = 0; $i < 10; $i++) {
// also you can use temp or useless variables
microSec($xyzabc);
sleep(1);
echo microSec($xyzabc) . " seconds"; // 1 second
}
你可以通过以下方式使用微时间(true):
把这个放在你的php文件的开头:
//place this before any script you want to calculate time
$time_start = microtime(true);
//你的脚本代码在这里
// do something
把这个放在你的php文件的末尾:
// 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';
它将在几分钟内输出结果。
下面是一个返回小数秒的实现(即1.321秒)
/**
* MICROSECOND STOPWATCH FOR PHP
*
* Class FnxStopwatch
*/
class FnxStopwatch
{
/** @var float */
private $start,
$stop;
public function start()
{
$this->start = self::microtime_float();
}
public function stop()
{
$this->stop = self::microtime_float();
}
public function getIntervalSeconds() : float
{
// NOT STARTED
if (empty($this->start))
return 0;
// NOT STOPPED
if (empty($this->stop))
return ($this->stop - self::microtime_float());
return $interval = $this->stop - $this->start;
}
/**
* FOR MORE INFO SEE http://us.php.net/microtime
*
* @return float
*/
private static function microtime_float() : float
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// 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";
?>
这是这个的链接
你可以使用微时间功能。从文档中可以看到:
microtime -以微秒为单位返回当前Unix时间戳 如果get_as_float被设置为TRUE,则microtime()返回一个浮点数,它表示当前时间(以秒为单位),从Unix纪元开始精确到最近的微秒。
使用示例:
$start = microtime(true);
while (...) {
}
$time_elapsed_secs = microtime(true) - $start;