检查脚本完成时间

时间:2012-02-22 16:35:02

标签: php mysql for-loop

我有以下示例:

// 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更新。如果你不了解我,你可以问我。我很抱歉这种语言。我英语不好,我不确定这件事是否可行。谢谢。

3 个答案:

答案 0 :(得分:4)

$start = microtime(true);

[...]

$end = microtime(true);

if (($end - $start) < 60) {

}

答案 1 :(得分: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

}

答案 2 :(得分: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