▶ 뭉충닷컴
 
mungchung.com login
Site Search
My Space
주절주절...
공부방
쥔장소개
Board
자유게시판
유용한 정보
Programming
강좌 & Articles
Spring 3.0
프로그래밍 Tip
   -ASP
   -PHP
   -JavaScript
   -HTML
   -Java/JSP
   -Database
   -Crystal Report
   -Visual Basic
   -이클립스
   -리눅스
   -기타
Windows API (VB)
Spread Sheet 7.0
컴퓨터활용 Tip
Other things
StarCraft 전략
StarCraft 문서
김용(金庸)
Son Q & Dieda

php 문법 성능 측정

PHP 조회 수 11854 추천 수 0 2010.01.29 23:12:21
출처 : http://www.phpbench.com/




Right now this page has a huge amount of attention by the international website programming community. Because of this I've had many many different opinions emailed to me about what people think about these results and how they were found. Keep sending them in.

This project has been a long time dream for me and I'd love to keep on learning about what I'm doing right / wrong and how we should approach this topic. If you have any information / opinions that you would like to share (especially about the larger bottleneck problems), please contact me via my portfolio.

Chris Vincent
PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates, but that is ok. This page was also created so that you would be able to find discovery in these statistics and then maybe re-run these tests in your own server environment to play around with this idea yourself, by using the code examples (these code examples are automatically generated and as the code in my .php files change, so do they).

NOTE: You must keep in mind to refresh this page a few times to "catch" the right result. The numbers change sometimes drastically during each refresh. I assume that this is because of PHP's memory garbage collector that drops in randomly and also other processes that run on this machine have an influence.

NOTE: The microtime() method for testing out these tests has only been utilised for simplicities sake. If anything more advanced was used, those whom are just starting out on their PHP journey would have difficulties understanding.

Counting LoopsFor vs. While

Is there an actual difference between counting up between the for loop and the while loop?

+ 106 %

for($i = 0; $i < 1000000; ++$i);

Total time: 95128 µsview code

+ 100 %

$i = 0; while($i < 1000000) ++$i;

Total time: 90001 µsview code

Conclusion:

Well there you have it, the while loop 90% of the time is indeed slightly faster

Using the =&-ref-operator$obj = new SomeClass() vs. $obj =& new SomeClass()

Is a good idea to use the =&-ref-operator when creating a new object? Call 1'000x

+ 106 %

$obj = new SomeClass();

Total time: 435 µsview code

+ 100 %

$obj =& new SomeClass();

Total time: 410 µsview code

Conclusion:

There seams to be no difference in performance.

String Outputecho vs. print

Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x

+ 100 %

echo ''

Total time: 75 µsview code

+ 113 %

print ''

Total time: 85 µsview code

+ 141 %

echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 106 µsview code

+ 145 %

print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 109 µsview code

+ 180 %

echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'

Total time: 135 µsview code

+ 333 %

echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'

Total time: 250 µsview code

+ 195 %

print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'

Total time: 146 µsview code

+ 478 %

$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a

Total time: 359 µsview code

+ 409 %

$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a

Total time: 307 µsview code

+ 491 %

$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a

Total time: 369 µsview code

+ 524 %

$a = 'aaaaaaa';
echo $a.$a.$a.$a

Total time: 394 µsview code

+ 419 %

$a = 'aaaaaaa';
echo $a,$a,$a,$a

Total time: 315 µsview code

+ 547 %

$a = 'aaaaaaa';
print $a,$a,$a,$a

Total time: 411 µsview code

Conclusion:

In reality the echo and print functions serve the exact purpose and therefore in the backend the exact same code applies. The one small thing to notice is that when using a comma to separate items whilst using the echo function, items run slightly faster.

Variable Type CheckingisSet() vs. empty() vs. is_array()

What is the performance of isSet() and empty(). Call 2'000x

+ 152 %

isSet() with var that was set

Total time: 245 µsview code

+ 144 %

empty() with var that was set

Total time: 231 µsview code

+ 103 %

isSet() with var that was *not* set

Total time: 166 µsview code

+ 103 %

empty() with var that was *not* set

Total time: 165 µsview code

+ 139 %

isSet() with array-var that was set

Total time: 224 µsview code

+ 154 %

empty() with array-var that was set

Total time: 248 µsview code

+ 101 %

isSet() with array-var that was *not* set

Total time: 163 µsview code

+ 100 %

empty() with array-var that was *not* set

Total time: 161 µsview code

+ 308 %

is_array() of an array

Total time: 495 µsview code

+ 320 %

is_array() of a string

Total time: 515 µsview code

+ 705 %

is_array() of a non set value

Total time: 1135 µsview code

+ 859 %

isSet() AND is_array() of a non set value

Total time: 1382 µsview code

Conclusion:

isSet() and empty() are identical. So alway check if val is set at all befor using type-checking. E.g. if (isSet($foo) AND is_array($foo))

Modify Loop: foreach() vs. for vs. while(list() = each())

What would happen if we alter the reading loop test to test the results of a loop created to simply alter the data in each of the values in the array?

Given again is a Hash array with 100 elements, 24byte key and 10k data per entry.

+ 395 %

foreach($aHash as $key=>$val) $aHash[$key] .= "a";

Total time: 178 µsview code

+ 146 %

while(list($key) = each($aHash)) $aHash[$key] .= "a";

Total time: 66 µsview code

+ 100 %

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";

Total time: 45 µsview code

Conclusion:

Proof in this example shows how functionally murderous the foreach() loop can be.

Control Structuresswitch/case/default vs. if/elseif/else

Is a there a difference between switch and if structures?. Call 1'000x

+ 132 %

if and elseif (using ==)

Total time: 172 µsview code

+ 130 %

if, elseif and else (using ==)

Total time: 169 µsview code

+ 106 %

if and elseif (using ===)

Total time: 137 µsview code

+ 100 %

if, elseif and else (using ===)

Total time: 130 µsview code

+ 131 %

switch / case

Total time: 170 µsview code

+ 138 %

switch / case / default

Total time: 179 µsview code

Conclusion:

Using a switch/case or if/elseif is almost the same. Note that the test is unsing === (is exactly equal to) and is slightly faster then using == (is equal to).

Using the =&-ref-operator$obj = $someClass->f() vs. $obj =& $someClass->f()

Is a good idea to use the =&-ref-operator when calling a function in an object? Call 1'000x

+ 100 %

$obj = $someClass->f();

Total time: 378 µsview code

+ 201 %

$obj =& $someClass->f();

Total time: 760 µsview code

Conclusion:

Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.

Using the &-ref-operator...as a so called "alias"

Is a good idea to use the &-ref-operator to substitute (or alias) a complex mutidim-array? . Call 1'000x

E.g. $person = &$aHach["country"]["zip"]["street"]["number"]["name"]

+ 213 %

$alias = $aSingleDimArray[$i]

Total time: 437 µsview code

+ 100 %

$alias = &$aSingleDimArray[$i]

Total time: 205 µsview code

+ 239 %

$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]

Total time: 491 µsview code

+ 352 %

$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]

Total time: 721 µsview code

+ 280 %

$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]

Total time: 573 µsview code

+ 960 %

$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]

Total time: 1968 µsview code

Conclusion:

Whilst only using a one dimensional array, it's actually faster to use an alias, but anything larger will result in a performance drop.

Read Loop:foreach() vs. for() vs. while(list() = each())

What is the best way to loop a hash array?

Given is a Hash array with 100 elements, 24byte key and 10k data per entry

+ 100 %

foreach($aHash as $val);

Total time: 15 µsview code

+ 713 %

while(list(,$val) = each($aHash));

Total time: 107 µsview code

+ 127 %

foreach($aHash as $key => $val);

Total time: 19 µsview code

+ 798 %

while(list($key,$val) = each($aHash));

Total time: 120 µsview code

+ 267 %

foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];

Total time: 40 µsview code

+ 946 %

while(list($key) = each($aHash)) $tmp[] = $aHash[$key];

Total time: 142 µsview code

+ 325 %

Get key-/ value-array: foreach($aHash as $key[]=>$val[]);

Total time: 49 µsview code

+ 306 %

Get key-/ value-array: array_keys() / array_values()

Total time: 46 µsview code

+ 405 %

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];

Total time: 61 µsview code

Conclusion:

In all cases I've found that the foreach loop is substantially faster than both the while() and for() loop procedures. One thing to note is that when using an entire loop from the start it's extremely good to use the reset() function in all examples

Given that the previous version of the tests have been very controvercial and incorrect, I must appologise for forgetting to implement the reset() function to allow the while() loops to start from the beginning instead of the end. Thanks to Anthony Bush for spotting this out.

Quote Typesdouble (") vs. single (') quotes

Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x

+ 107 %

single (') quotes. Just an empty string: $tmp[] = '';

Total time: 398 µsview code

+ 105 %

double (") quotes. Just an empty string: $tmp[] = "";

Total time: 389 µsview code

+ 115 %

single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';

Total time: 425 µsview code

+ 101 %

double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";

Total time: 374 µsview code

+ 100 %

single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';

Total time: 371 µsview code

+ 101 %

double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";

Total time: 374 µsview code

+ 105 %

double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";

Total time: 389 µsview code

Conclusion:

In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!

Counting LoopsFor-loop test

Is it worth the effort to calculate the length of the loop in advance?

e.g. "for ($i=0; $i<$size; $i++)" instead of "for ($i=0; $i<sizeOf($x); $i++)"

A loop with 1000 keys with 1 byte values are given.

+ 100 %

With pre calc - count()

Total time: 233 µsview code

+ 34453 %

Without pre calc - count()

Total time: 80336 µsview code

+ 101 %

With pre calc - sizeof()

Total time: 236 µsview code

+ 51834 %

Without pre calc - sizeof()

Total time: 120862 µsview code

Conclusion:

Unsurprising results... this is one of the easiest things to implement in any application and is the widest agreed upon benchmarking item within the online PHP community. The results basically speak for themselves.

문서 첨부 제한 : 0Byte/ 2.00MB
파일 크기 제한 : 2.00MB (허용 확장자 : *.*)
List of Articles
번호 제목 글쓴이 날짜 조회 수
491 JAVA 개발자가 놓치기 쉬운 자바의 개념, 기본원리 뭉충닷컴 2012-03-01 15820
490 이클립스 maven 빌드시 Failed to execute goal org.apache.maven.plugins 이런 오류가 날 경우 file 뭉충닷컴 2012-02-24 7009
489 이클립스 이클립스 최적화 및 속도개선 뭉충닷컴 2012-02-07 9369
488 기타 SVN의 RA layer request failed 에러처리 뭉충닷컴 2012-02-03 9505
487 JAVA java.io.CharConversionException: EOF 뭉충닷컴 2012-01-30 12289
486 JAVA jar 분석 할때 사용하는 툴 file 뭉충닷컴 2012-01-30 7165
485 JAVA 하위디렉토리까지 찾아서 특정파일 찾아오기 뭉충닷컴 2012-01-18 5808
484 DataBase connect by를 이용한 몇가지 쿼리 뭉충닷컴 2011-11-29 5575
483 JAVA jvm 관련해서 쉽게 정리한 글 뭉충닷컴 2011-10-21 6308
482 리눅스 chkconfig 및 runlevel 뭉충닷컴 2011-10-19 5866
481 DataBase 오라클 with를 이용한 인라인뷰 뭉충닷컴 2011-05-26 9860
480 Script showModalDialog 와 showModelessDialog 의 아주 유용한 속성들 [1] 헤븡 2011-03-07 14787
479 ASP asp Explorer file [4] 헤븡 2011-03-03 11779
478 리눅스 find 명령어 여러가지 사용법 (리눅스 파일 찾기) 뭉충닷컴 2011-01-21 16967
477 리눅스 SSL 비밀번호가 다른 2개의 웹서비스를 하나의 아파치 서버에 셋팅 방법 뭉충닷컴 2011-01-12 11454
476 기타 command/console 지원 ftp 프로그램 file 뭉충닷컴 2011-01-12 9765
475 기타 ActiveX method 분석 방법 file 뭉충닷컴 2010-12-23 10382
474 기타 IEPrint 사용하기 (웹 인쇄시 여백 및 대화상자 설정) file [1] 뭉충닷컴 2010-12-23 13699
473 Script 문자를 아스키코드로 변환, 아스키코드를 문자로 변환 뭉충닷컴 2010-12-22 19406
472 리눅스 리눅스에서 서비스 설정하기. 뭉충닷컴 2010-12-14 9476
471 리눅스 tar 압축/해제의 여러가지 사용법 뭉충닷컴 2010-12-14 10684
470 기타 톰캣 로드밸런싱 설정하기 뭉충닷컴 2010-12-14 9650
469 DataBase 컬럼에 한글 포함여부 검사 뭉충닷컴 2010-09-29 13731
468 리눅스 아파치 로그파일 크기가 1기가 이상되면 백업하기 뭉충닷컴 2010-09-29 11387
467 Script script 인클루드시 src의 쿼리스트링(파라미터)값 읽어들이기 [1] 뭉충닷컴 2010-09-28 10237
466 Script chrome, opera등 브라우저에서 xml의 selectNodes, selectSingleNode 사용하기 뭉충닷컴 2010-08-25 14645
465 기타 svn.exe (svn command line) 뭉충닷컴 2010-08-20 10354
464 DataBase 오라클 숨겨진 함수들 뭉충닷컴 2010-07-29 13502
463 HTML 기본 css 셀렉터 뭉충닷컴 2010-07-20 9498
462 이클립스 이클립스에서 톰캣 메모리 설정 뭉충닷컴 2010-06-29 17448
461 DataBase 오라클 암시적 커서 사용 예제 뭉충닷컴 2010-06-11 11789
460 DataBase 오라클 Function 에서 DML 문 사용하기 뭉충닷컴 2010-06-10 14552
459 Script vbscript 실행 시간 지연 뭉충닷컴 2010-06-09 15812
458 DataBase EXECUTE IMMEDIATE 사용 예제 뭉충닷컴 2010-05-28 13024
457 Script 간단한 dom 노드 탐색 뭉충닷컴 2010-04-19 10583
456 DataBase dblink를 통한 insert시 속도향상 뭉충닷컴 2010-04-08 19658
455 Script Script 메모리 누수에 대한 TIP [1] 뭉충닷컴 2010-03-12 14459
454 DataBase 오라클 DB의 프로시저, 함수, 시노님 등등 목록 보기 뭉충닷컴 2010-03-11 14797
453 Script ie에서 경고창 없이 창 닫기 뭉충닷컴 2010-02-19 15592
452 Script 한글 자소 분석하여 문자열처리 [1] 뭉충닷컴 2010-02-16 11884
451 JAVA 한글 자소 분리 [1] 뭉충닷컴 2010-02-16 13319
450 Script 한글 자소 분리 뭉충닷컴 2010-02-16 11071
449 기타 에디트 플러스(editplus)용 zen coding file [2] 뭉충닷컴 2010-02-05 17856
448 리눅스 500 OOPS: vsftpd: cannot locate user specified in 'ftp_username':ftp 뭉충닷컴 2010-02-01 12822
» PHP php 문법 성능 측정 뭉충닷컴 2010-01-29 11854
446 Script 현재실행된 함수명을 호출 뭉충닷컴 2010-01-25 12294
445 JAVA 자바 소수점 자릿수 값 가져오기 뭉충닷컴 2010-01-14 19224
444 HTML 아이폰/아이팟용 웹사이트 만들 때 알아둬야 할 기본 뭉충닷컴 2010-01-06 11928
443 DataBase 오라클에서 특정문자개수 구하기 [1] 뭉충닷컴 2009-12-30 21696
442 Script 자바스크립트로 layer(span, div, iframe) 사이즈 조절 막대 file 뭉충닷컴 2009-12-21 16243
441 리눅스 (13) Permission denied Error 뭉충닷컴 2009-11-30 15658
440 HTML 프레임(frame)위에 레이어(layer) 띄우기 file 뭉충닷컴 2009-11-27 16080
439 HTML 레이어 팝업 띄워서 마우스로 이동 file 뭉충닷컴 2009-11-26 14941
438 리눅스 wrong ELF class: ELFCLASS32 오류 해결하기 [2] 뭉충닷컴 2009-11-24 14650
437 리눅스 로그인시 쉘스크립트 변경 [1] 뭉충닷컴 2009-11-23 10774
436 JAVA [JOTM+Xapool] xapool 이용 시 Null Pointer Exception 문제 해결 file 뭉충닷컴 2009-11-10 11875
435 JAVA getRequestURL jsp / servlet / java 현재 경로 알아내기 뭉충닷컴 2009-11-09 30829
434 리눅스 리눅스 터미널에 디렉토리, 파일별로 색상주기 뭉충닷컴 2009-11-09 14784
433 리눅스 리눅스에서 심볼릭 링크 만 찾기 뭉충닷컴 2009-11-07 12373
432 기타 톰캣의 org.apache.jk.common.ChannelSocket processConnection 오류 해결하기 뭉충닷컴 2009-11-04 15286