【问题标题】:How to calculate execution time (php directive 'max_execution_time'? )如何计算执行时间(php指令'max_execution_time'?)
【发布时间】:2012-01-18 03:19:17
【问题描述】:

几天前写了一个 php 脚本,它遍历我所有的音乐并读取每首歌曲的 id3 标签并将其插入到 mysql 数据库中。这是上下文的 sn-p:

exec ( "find /songs -type f -iname '*mp3'", $song_path );
$number_of_songs = count($song_path);
for($i=0; $i<$number_of_songs; $i++){
    //read id3 tags
    //store id3 tags into database
}

我更改了 apache2/php.ini 中的 php 指令 max_execution_time 以更好地了解该指令的作用。

当我设置max_execution_time = 10 时,我的 php 脚本运行了大约 45 秒,并成功读取了大约 150 首歌曲(数千首歌曲中)的 id3 标签,并将这些标签插入到 mysql 数据库中,然后终止脚本并输出跟随屏幕:

致命错误:/websites/.../public_html/GetID3()/getid3/module.audio.mp3.php 第 1894 行中超过 10 秒的最大执行时间

从文档来看,'最大执行时间不受系统调用、流操作等影响' http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

  1. 我可以从两者的区别中推断出什么 maximum_execution_time 设置为 10 秒和脚本 在终止前总共运行了 45 秒?做这个 平均在 45 秒内,有 35 秒用于与 PHP 无关的操作 诸如读取 id3 标签、将数据插入 mysql 等活动......,而 10 被花费 做 php 相关的活动,比如迭代循环?

  2. 有没有办法可以计算执行时间并将其打印到 屏幕?

编辑 使用 Dagon 建议的计时器,我在循环结束时调用了 getTime() 函数,循环大约有 100 多次迭代。这是我浏览器的输出:

0.1163 秒

0.8142 秒

1.1379 秒

1.5555 秒

...

76.7847 秒

77.2008 秒

77.6071 秒

致命错误:/websites/.../public_html/GetID3()/getid3/module.audio.mp3.php 第 505 行中超过 10 秒的最大执行时间

【问题讨论】:

  • 你碰巧在使用 set_time_limit (php.net/set_time_limit) 吗? '调用时,set_time_limit() 从零重新启动超时计数器。换句话说,如果超时是默认的 30 秒,并且在脚本执行 25 秒后调用了诸如 set_time_limit(20) 之类的调用,则脚本将在超时之前总共运行 45 秒。'
  • 不,我没有使用 set_time_limit

标签: php bash shell


【解决方案1】:
<!-- Paste this at the top of the page -->
<?php
   $mic_time = microtime();
   $mic_time = explode(" ",$mic_time);
   $mic_time = $mic_time[1] + $mic_time[0];
   $start_time = $mic_time;
?>

<!-- Write Your script(executable code) here  -->

enter code here

<!-- Paste  this code at the bottom of the page -->
<?php
   $mic_time = microtime();
   $mic_time = explode(" ",$mic_time);
   $mic_time = $mic_time[1] + $mic_time[0];
   $endtime = $mic_time;
   $total_execution_time = ($endtime - $start_time);
   echo "Total Executaion Time ".$total_execution_time." seconds";
?>

【讨论】:

    【解决方案2】:

    我不相信脚本实际运行超过 10 秒,您需要在其中放置一个适当的计时器

    <!-- put this at the top of the page -->
    <?php
    function getTime() {
        $timer = explode( ' ', microtime() );
        $timer = $timer[1] + $timer[0];
        return $timer;
    }
    
    $start = getTime();
    ?> 
    
    <!-- put other code and html in here --> 
    
    <!-- put this code at the bottom of the page -->
    <?php
    $end = getTime();
    $time_took=  round($end - $start,4).' seconds';
    echo $time_took;
    ?>
    

    【讨论】:

      【解决方案3】:

      这种类型的脚本确实应该在 CLI 环境中执行,而不是在您的 Web 服务器执行的 php 进程中。根据 manual docs 关于 PHP 命令行环境与其他 PHP SAPI 的不同之处:

      shell 环境中的 PHP 倾向于用于更多样化的 目的范围比典型的基于 Web 的脚本,因为这些可以 运行时间很长,最长执行时间设置为无限制

      虽然它不能回答你的问题,但它确实解决了你的问题:)

      【讨论】:

      • 嘿,谢谢伙计。我通过 cli 处理脚本,但是在这种情况下,我想创建一个用户可以登录/注销的网站,以便我可以解析他们的 id3 标签,这就是我从 cgi 测试它的原因。
      • 顺便说一句,你像亚当莱文:栗色5的歌手
      • @LedZeppelin 哈哈,当我住在洛杉矶的时候,我经常在深夜喝醉的女孩说同样的话。至于您的网站用例……您最好生成一个新进程来处理标签解析。这样您仍然可以利用“CLI”环境。此外,对于这种类型的处理作业,您应该考虑使用某种队列系统,因为这样的长处理作业不太适合单个 Web 服务器 php 进程。
      • 感谢您的建议,如果我完成了这个项目,我会搬到亚马逊 ec2 并获得一些高 cpu 预留实例,但现在我只是在我的服务器上动手房间。
      【解决方案4】:

      似乎您不仅尝试测量脚本持续时间,还尝试限制它。在你的情况下,max_execution_time 不是你想要的。

      基本上“最大执行时间不受系统调用、流操作等影响”是正确的。如果需要限制实时脚本长度,则需要实现自己的时间测量。人们通常会为它写一些基准类,毕竟这将有助于优化脚本,但简单

      $timer['start'] = time();
      $timer['max_exec_time'] = 10; // seconds
      

      在开始和

      if (time() > $timer['start'] + $timer['max_exec_time'])
          break; // or exit; etc
      

      在循环结束或任何你想要的地方就足够了。

      【讨论】:

        猜你喜欢
        • 2011-08-06
        • 2013-04-30
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多