【问题标题】:Scripts execution time脚本执行时间
【发布时间】:2013-06-07 04:11:06
【问题描述】:

我的问题基于Accurate way to measure execution times of php scripts

问题是如果我有一个很长的代码,其中有很多 die() 或返回或退出;功能

我不想计算脚本执行速度(可能不同,取决于参数..)

有什么办法吗?

我的建议是:

<?php
$time_start = microtime(true); 
include("script_real_name.php");
$time_end=microtime(true);
$dif=$time_end-$time_start;

/* 但问题是,这个脚本是否适用于 DIE() 或 EXIT;在“script_real_name.php”上调用的函数?

如何使它可行。

还有其他问题,包含的脚本可以与 $_POST、$_GET 一起使用吗?

*/

【问题讨论】:

标签: php performance time execution


【解决方案1】:

您可以在函数中进行计算,并使用register_shutdown_function 注册该函数以便在关机时执行。

$time_start = microtime(true);

register_shutdown_function(function() use ($time_start) {
    $time_end= microtime(true);
    $dif = $time_end-$time_start;
    echo "Script ran for $dif seconds\n";
});

// do a lot of work and die() suddenly and somewhere

【讨论】:

    【解决方案2】:

    如果你有 linux shell 访问权限,你可以试试

    $ time php myscript.php
    

    除此之外,您可以在脚本开头将脚本开始时间定义为 CONST,并使用您的逻辑在执行结束后回显持续时间。 “什么”——你可能会说——“在 php die()d 之后运行命令?”是的。注册一个函数到run after shutdown.

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多