Symfony1 当前执行时间?

Symfony1 当前执行时间?,symfony1,symfony-1.4,Symfony1,Symfony 1.4,我有一个耗时的脚本,我想定期记录它的执行时间。如何找出当前的执行时间?为什么不使用日期('Y-m-d H:I:s')并记录它。。。和/或计算与使用date()获得的开始时间的差异?想想你在问什么,每个操作都记录在symfony日志(root/log/app_name_u[env].log)中。此日志记录在操作结束后完成(没有简单的方法计算从php执行php的线程的执行时间)。您可以尝试弄乱代码并添加代码,以便在代码的某些点、当前执行时间进行日志记录,例如: $init = microtime()

我有一个耗时的脚本,我想定期记录它的执行时间。如何找出当前的执行时间?

为什么不使用
日期('Y-m-d H:I:s')
并记录它。。。和/或计算与使用date()获得的开始时间的差异?

想想你在问什么,每个操作都记录在symfony日志(root/log/app_name_u[env].log)中。此日志记录在操作结束后完成(没有简单的方法计算从php执行php的线程的执行时间)。您可以尝试弄乱代码并添加代码,以便在代码的某些点、当前执行时间进行日志记录,例如:

$init = microtime();
log("Process started at:". $init);
foreach($this->getLotsOfRecords() as $index=>$record)
{
  $start = microtime();
  log ($index." record started at".microtime());
  [do stuff]
  $end = microtime();
  log ($index." record ended at". $end . " time elapsed: ". ($start - $end));
}
$last = microtime();
log("Total elapsed time is: ". ($init - $last));

这是伪代码,但我相信您可以找出其余的,希望这有帮助

我最终编写了自己的静态类:

class Timer
{
  protected static $start = null;

  public static function getDuration($format = '%H:%I:%S')
  {
    $now = new DateTime();

    if (!self::$start)
    {
      self::$start = new DateTime();
    }
    $period = $now->diff(self::$start);

    return $period->format($format);
  }
}
并将其部分记录(即循环):


symfony记录执行时间的方法是使用symfony附带的计时器管理器

//Get timer instance called 'myTimer'.
$timer = sfTimerManager::getTimer('myTimer');
//Start timer.
$timer->startTimer();
// Do things
...
// Stop the timer and add the elapsed time
$timer->addTime();
此计时器将保存到您使用symfony配置的任何记录器中。 默认情况下,symfony具有用于DEV环境的sfWebDebugLogger,但是您可以创建自己的,并在
factories.yml
文件中配置它

这个记录器的优点是它还记录了对计时器的调用次数

//Get timer instance called 'myTimer'.
$timer = sfTimerManager::getTimer('myTimer');
//Start timer.
$timer->startTimer();
// Do things
...
// Stop the timer and add the elapsed time
$timer->addTime();