我想知道执行一个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
发布于 2013-08-20 21:58:14
您可以通过以下方式使用microtime(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';
它将以minutes
格式输出结果。
发布于 2013-03-18 23:46:28
创建文件loadtime.php
<?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秒内加载”。
发布于 2011-06-06 05:29:16
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
// do something
}
$total = microtime(true) - $start;
echo $total;
https://stackoverflow.com/questions/6245971
复制相似问题