저는 얼마 전이 글을 쓰는 Timer 클래스를 사용합니다. 장점은 "증분"이므로 타이머 을 시작하여 루프를 시작하고 시작과 중지 사이에 시간을 추가합니다.
이렇게하면 실행하는 데 시간이 상당히 걸릴 수 있습니다. 기본적인 사용법 :
$myTimer = new Timer();
$myTimer->start('hello'); // $myTimer = new Timer('hello'); is a shorthand for this.
for ($i=0; $i<100000; $i++)
{
$myTimer->start('slow suspect 1');
// stuff
$myTimer->stop('slow suspect 1');
$myTimer->start('slow suspect 2');
// moar stuff
$myTimer->stop('slow suspect 2');
}
$myTimer->stop('hello');
$myTimer->print_all();
이 그것을 할 수있는 가장 빠른 방법을 제한이고 멀지 않은 유의하시기 바랍니다. 수업을 만들고 파괴하는 데는 시간이 걸립니다. "논리"루프 내부에서 완료되면 꽤 많은 시간을 추가 할 수 있습니다. 하지만 여러 겹침 루프 또는 재귀 함수 호출을 일부 복잡한 프로그램을 추적,이 물건은, 그러나,
<?php
class Timer_
{
public $start;
public $stop;
public function __construct()
{
$this->start = microtime(true);
}
}
class Timer
{
private $timers = array();
public function __construct($firsTimer=null)
{
if ($firsTimer != null) $this->timers[$firsTimer][] = new Timer_();
}
public function start($name)
{
$this->timers[$name][] = new Timer_();
}
public function stop($name)
{
$pos = count($this->timers[$name]) -1 ;
$this->timers[$name][$pos]->stop = microtime(true);
}
public function print_all($html=true)
{
if ($html) echo '<pre>';
foreach ($this->timers as $name => $timerArray)
{
$this->print_($name, $html);
}
if ($html) echo '</pre>';
}
public function print_($name, $html=true)
{
$nl = ($html) ? '<br>' : NEWLINE;
$timerTotal = 0;
foreach ($this->timers[$name] as $key => $timer)
{
if ($timer->stop != null)
{
$timerTotal += $timer->stop - $timer->start;
}
}
echo $name, ': ', $timerTotal, $nl;
}
}
?>
당신은 내가 일반적으로 할 무엇 무엇 –
을 변경하지 않고 "여기 저쩌구"블록 내에서 OOP를 사용할 수 있습니다 소중합니다 더 큰 프로젝트의 경우 정확한 시간을 원한다면 그렇다면 $ timestart가 조기에 호출되고 작은 PHP 파일에 있는지 확인하는 것이 중요합니다. PHP 파일을 포함하여 속도가 느릴 수도 있기 때문입니다. 또한 PHP 5.3을 사용하는 경우 (또는 일부 최신 버전), 페이지 요청 타임 스탬프가 마이크로 시간 "형식"으로 변경되었습니다. 더 정확하고 $ timestart가 필요하지 않습니다. – Andreas
감사합니다. 그것을 사용하는 것이 정말 재미있는가요? 또는 스크립트가 얼마나 실행되는지를 하루 만 분석하고자 할 때만 가능합니다. – Zeroth