【问题标题】:Calculate execution time of php command execution (popen() function)计算php命令执行的执行时间(popen()函数)
【发布时间】:2020-05-02 20:01:19
【问题描述】:

我想计算PHP中popen函数的执行时间。该函数默认不提供执行时间。

我的伪代码如下;

    $handle        = popen($cmd, 'r');
    while (!feof($handle)) {
        $buffer = fgets($handle, FREAD_BUFFER_SIZE);
        $out .= $buffer . " ";
    }

我在here 中找到了解决方案,但我不确定它是否正确。

【问题讨论】:

  • 你试过了吗?
  • 它有助于限制在 while 循环中设置的超时。实际上,如果进程持续时间比超时时间长,它就不起作用了。

标签: php process


【解决方案1】:

考虑使用 time() 或 microtime(true) 函数。

$currentTimeinSeconds = microtime(true);

这会存储从它声明的实例到 1970 年 1 月 1 日所经过的秒数。制作两个变量(例如 T1 和 T2),然后将第二个变量减去第一个变量即可得到执行时间。

$t1= microtime(true)
[function]
$t2= microtime(true)
ex_time=$t2-$t1

【讨论】:

  • 你可能想要microtime(true)
  • 其实很简单,我不知道,我怎么忘记了,但是非常感谢
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 2010-11-13
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
相关资源
最近更新 更多