我想知道执行一个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
当前回答
这里有一个函数,可以计时PHP代码的任何部分的执行,很像Python的timeit模块:https://gist.github.com/flaviovs/35aab0e85852e548a60a
如何使用:
include('timeit.php');
const SOME_CODE = '
strlen("foo bar");
';
$t = timeit(SOME_CODE);
print "$t[0] loops; $t[2] per loop\n";
结果:
$ php x.php
100000 loops; 18.08us per loop
免责声明:我是本文主旨的作者
EDIT: timeit现在是https://github.com/flaviovs/timeit上一个独立的、独立的项目
其他回答
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
// do something
}
$total = microtime(true) - $start;
echo $total;
您可以用一个函数找到以秒为单位的执行时间。
// 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
}
我想分享一下我的功能。希望能帮你节省时间。
它最初用于跟踪基于文本的脚本的时间,因此输出是文本形式的。但如果您愿意,可以轻松地将其修改为HTML。
它将为您计算自脚本开始和在每个步骤中花费了多少时间。它将所有输出格式化为3个精度小数。(精确到毫秒。)
一旦将它复制到脚本的顶部,所要做的就是将recordTime函数调用放在想要计时的每个片段之后。
复制到你的脚本文件的顶部:
$tRecordStart = microtime(true);
header("Content-Type: text/plain");
recordTime("Start");
function recordTime ($sName) {
global $tRecordStart;
static $tStartQ;
$tS = microtime(true);
$tElapsedSecs = $tS - $tRecordStart;
$tElapsedSecsQ = $tS - $tStartQ;
$sElapsedSecs = str_pad(number_format($tElapsedSecs, 3), 10, " ", STR_PAD_LEFT);
$sElapsedSecsQ = number_format($tElapsedSecsQ, 3);
echo "//".$sElapsedSecs." - ".$sName;
if (!empty($tStartQ)) echo " In ".$sElapsedSecsQ."s";
echo "\n";
$tStartQ = $tS;
}
想要记录时间的流逝,你可以这样做:
recordTime("What We Just Did")
例如:
recordTime("Something Else")
//Do really long operation.
recordTime("Really Long Operation")
//Do a short operation.
recordTime("A Short Operation")
//In a while loop.
for ($i = 0; $i < 300; $i ++) {
recordTime("Loop Cycle ".$i)
}
给出如下输出:
// 0.000 - Start
// 0.001 - Something Else In 0.001s
// 10.779 - Really Long Operation In 10.778s
// 11.986 - A Short Operation In 1.207s
// 11.987 - Loop Cycle 0 In 0.001s
// 11.987 - Loop Cycle 1 In 0.000s
...
// 12.007 - Loop Cycle 299 In 0.000s
希望这能帮助到一些人!
你可以通过以下方式使用微时间(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';
它将在几分钟内输出结果。
看()。