我想知道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
Run Code Online (Sandbox Code Playgroud)
Tim*_*per 486
microtime- 返回当前的Unix时间戳,以微秒为单位
如果
get_as_float设置为TRUE,则microtime()返回一个浮点数,表示自Unix纪元精确到最接近的微秒以来的当前时间(以秒为单位).
用法示例:
$start = microtime(true);
while (...) {
}
$time_elapsed_secs = microtime(true) - $start;
Run Code Online (Sandbox Code Playgroud)
Adi*_*att 80
您可以使用microtime(true)以下方式:
把它放在你的php文件的开头:
//place this before any script you want to calculate time
$time_start = microtime(true);
Run Code Online (Sandbox Code Playgroud)
//你的脚本代码就在这里
// do something
Run Code Online (Sandbox Code Playgroud)
把它放在你的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';
Run Code Online (Sandbox Code Playgroud)
它会输出你的结果minutes.
Iwa*_*aru 68
您可以使用REQUEST_TIME从$_SERVER超全局数组.从文档:
'REQUEST_TIME'
请求开始的时间戳.从PHP 5.1.0开始提供.'REQUEST_TIME_FLOAT'
请求开始的时间戳,精度为微秒.从PHP 5.4.0开始提供.
这样,您无需在脚本开头保存时间戳.你可以简单地做:
<?php
// Do stuff
usleep(mt_rand(100, 10000));
// At the end of your script
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did stuff in $time seconds\n";
?>
Run Code Online (Sandbox Code Playgroud)
这里,REQUEST_TIME将包含自脚本开始以来经过的时间(以秒为单位),精确度REQUEST_TIME_FLOAT为微秒(例如1秒和341微秒)
Sol*_*ili 27
创建文件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";
}
}
Run Code Online (Sandbox Code Playgroud)
<?php写完之后,比你脚本的开始include 'loadtime.php'; $loadtime=new loadTime();
当页面在最后加载时,将写入"在x秒内加载"
Waq*_*leh 26
我这样做的方法是使用hrtime,它是为性能指标创建的,并且独立于系统时间。hrtime不受系统时间变化的影响。hrtime自 PHP 7.3.0 起可用
$start = hrtime(true);
sleep(5); // do something, in your case a loop
$end = hrtime(true);
$eta = $end - $start;
// convert nanoseconds to milliseconds
$eta /= 1e+6;
echo "Code block was running for $eta milliseconds";
Run Code Online (Sandbox Code Playgroud)
输出:
Code block was running for 5000.495206 milliseconds
Dvi*_*lay 18
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
// do something
}
$total = microtime(true) - $start;
echo $total;
Run Code Online (Sandbox Code Playgroud)
这是一个函数,可以执行任何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";
Run Code Online (Sandbox Code Playgroud)
结果:
$ php x.php
100000 loops; 18.08us per loop
Run Code Online (Sandbox Code Playgroud)
免责声明:我是这个要点的作者
编辑:timeit现在是一个独立的,自包含的项目,网址是https://github.com/flaviovs/timeit
你有正确的想法,除了microtime()函数有更精确的时间.
如果循环内部的内容很快,则表观经过的时间可能为零.如果是这样,请在代码周围再换一个循环并重复调用它.务必将差异除以迭代次数以获得每次一次.我有配置代码,需要10,000,000次迭代才能获得一致,可靠的时序结果.
| 归档时间: |
|
| 查看次数: |
226630 次 |
| 最近记录: |