"PHP 수행시간 측정"의 두 판 사이의 차이

5번째 줄: 5번째 줄:
==수행시간 측정==
==수행시간 측정==
<source lang='php'>
<source lang='php'>
<!DOCTYPE html>
<meta charset="utf-8" />
<?php
<?php
function get_time() {
function get_time() {

2012년 2월 26일 (일) 21:52 판

  • PHP 수행시간 측정
  • PHP 속도 측정
  • PHP 속도 비교

1 수행시간 측정

<!DOCTYPE html>
<meta charset="utf-8" />
<?php
function get_time() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$start = get_time();
/*
수행할 내용
*/
$end = get_time();
$time = $end - $start;
echo '<br/>'.$time.'초 걸림';
?>

2 수행시간 측정 예제

아래 소스코드 중 속도 측정 부분에 있는 pow를 pow2 또는 pow3로 바꾸어 함수의 수행시간(즉 성능)을 측정해 볼 수 있다. 여기서는 10만회 반복 수행한 시간을 측정한다.

<?php
function get_time() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

function pow2($b, $n)
{
	$result = 1;
	for($i=0;$i<$n;$i++) {
		$result *= $b;
	}
	return $result;
}

function pow3($b, $n)
{
	if($n<2)return $b;
	return pow3($b, $n-1) * $b;
}

$start = get_time();
// 속도 측정 시작

for($i=0;$i<100000;$i++) {
	$a = pow($i, 20);
}

// 속도 측정 끝
$end = get_time();
$time = $end - $start;
echo '<br/>수행시간: '.number_format($time,4).'초 걸림';
echo '<br/>결과값: '.$a;
?>
수행결과
수행시간: 0.0540초 걸림
결과값: 9.9980001899886E+99

→ 수행시간은 서버의 성능에 따라 달라진다.

3 예제 2

4 참고자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}