【问题标题】:Check script finish time检查脚本完成时间
【发布时间】:2012-03-13 00:37:58
【问题描述】:

我有下面的例子:

// start time
// make mysql query here

for($i = 0; $i < 10; $i++) // for loop
{
// make dig lookup here

   if (domain found){
    // Update database
    }

}

if (time <= 60 sec)// check current finishing time and compare
{
// make my sql update
}

我想要做的是检查循环后的时间是多少秒,然后如果完成 for 循环所用的时间少于 60 秒,则运行 mysql 更新。如果你不明白我,你可以问我。我为语言感到抱歉。我的英语不好,我不确定这件事是否可行。谢谢。

【问题讨论】:

    标签: php mysql for-loop


    【解决方案1】:

    首先,记得关闭你的连接。

    不管怎样,@webbiedave 给出的microtime 解决方案很好,但我有一些奇怪的经历(主要是负面结果)。

    Advanced PHP Debugger 给了我更好的结果。

    如何使用:

    1.脚本的第一行

    <?php
      // In the First line of your script
      apd_set_pprof_trace();
      // The rest of your code here
    ?>
    

    2.运行脚本。将生成此文件:apd.dumpdir/pprof_pid.ext

    3.用pprofp显示数据

    bash-2.05b$ pprofp -R /tmp/pprof.22141.0
    

    【讨论】:

    • 您有使用APD 的示例代码吗?我很想看看它,也可以给它+1。
    • 我不认为这是他所要求的。他希望自动完成此操作,而不必进入查看文件,然后手动运行更新。我认为在这种情况下,microtime 是要走的路,这不是 APD 的位置。
    • 是的,你是对的。但我认为在给定处理时间的情况下执行某些操作不是一个好主意。只需锁定所涉及的表即可改变一切。不管怎样,看看 APD 文档,它是一个很棒的工具!
    【解决方案2】:
    $time1 = time();
    for($i = 0; $i < 10; $i++){ // for loop
    
        // make dig lookup here
    
    }
    $time2 = time();
    
    if (($time2 - $time1) < 60){
    
        // make my sql update
    
    }
    

    【讨论】:

      【解决方案3】:
      $start = microtime(true);
      
      [...]
      
      $end = microtime(true);
      
      if (($end - $start) < 60) {
      
      }
      

      【讨论】:

      • @Jleagle:仅当您想检查 60,000 秒时。来自手册:If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      相关资源
      最近更新 更多