'php performance'에 해당되는 글 1건

  1. 2007.08.22 PHP Performance

PHP Performance

ITWeb/개발일반 2007.08.22 14:46

Reference Blog

PHP Performance

  • 디렉토리나 파일은 가능한 짧게 유지 하라.
  • FollowSymLinks 옵션을 사용하라.
  • logging 은 한파일에 하고 분석시 logging 을 중지 하라.
  • KeepAliveTimeout 은 가능한 낮게 잡아라.
  • static / dynamic request 를 구분하라.
  • output buffering 은 브라우저의 rendering 을 빠르게 해준다.
    • ob_start()
    • output_buffering=on
    • SendBufferSize = PageSize ( in apache )
    • memory size 가 작은 시스템에서는 사용하지 말라.
for($i = 0; $i < 1000; $i++) { echo $i; }

This method is much slower than something like this:

ob_start(); for($i = 0; $i <1000; $i++) { echo $i; } ob_end_flush();

    • 1.36509799957 without ob_start()
    • .248747825623 with ob_start()
  • bandwidth optimization
    • server 자원을 줄인다.
    • client page load 를 빠르게 한다.
    • network io 를 줄인다.
  • compression 은 cpu 의 3-5% 의 load 를 줄인다.
    • apache 1 : mod_gzip
    • apache 2 : mod_deflate
  • Tuning PHP Configuration
    • register_globals = Off **
    • magic_quotes_gpc = Off
    • expose_php = Off
    • register_argc_argv = Off
    • always_populate_raw_post_data = Off **
    • session.use_trans_sid = Off **
    • session.auto_start = Off **
    • session.gc_divisor = 1000 or 10000
    • output_buffering = 4096
  • Tuning PHP File Access
    • Inefficient Approach : < ? php include "file.php"; ? >
    • Performance Friendly Approach : < ? php include "/path/to/file.php"; or include "./file.php"; ? >
  • Regular Expression
    • Slow
      • if (preg_match("!^foo_!i", "FoO_")) { }
      • if (preg_match("![a8f9]!", "sometext")) { }
    • Faster
      • if (strncasecmp("foo_", "FoO_", 4)) { }
      • if (strpbrk("a8f9", "sometext")) { }
  • Optimizing str_replace()
    • replacement 할 필요가 없는 것들을 제거 하고 사용할것
      • 바꿔야 할 문자열과 대치해야할 문자열 중 필요 없는건 제거 할것
  • strtr() vs str_replace()
    • str_replace 가 strtr 보다 10배 정도 빠르다.
  • fopen() vs file_get_contents()
    • file_get_contents() 가 훨씬 간단하고 빠르다.
  • 쓰면 유용한 함수들
    • file_put_contents()
      • Append data to files or create new files in one shot.
    • microtime() and gettimeofday()
      • Return floats when passed TRUE as a 1st argument.
    • mkdir()
      • Can create directory trees, when 2nd arg. is TRUE.
    • glob()
      • Fetch all array of files/directories in one shot.
    • convert_uuencode,convert_uudecode
      • Fast UU encoding/decoding mechanism.
    • http_build_query()
      • Build GET/POST query based on associated array.
    • substr_compare()
      • strcmp/strncasecmp/etc… from an offset.
    • array_walk_recursive()
      • Recursively iterate through an array.
    • convert_uuencode,convert_uudecode
      • Fast UU encoding/decoding mechanism.
    • http_build_query()
      • Build GET/POST query based on associated array.
    • substr_compare()
      • strcmp/strncasecmp/etc… from an offset.
    • array_walk_recursive()
      • Recursively iterate through an array.
  • Multi-dimensioal Array trick
    • slow 2 extra hash lookups per access
      • $a['b']['c'] = array();
      • for($i = 0; $i < 5; $i++) $a['b']['c'][$i] = $i;
    • much faster reference based approach
      • $ref =& $a['b']['c'];
      • for($i = 0; $i < 5; $i++) $ref[$i] = $i;
  • Static 선언은 50-75% 의 성능 향상을 준다.
  • 불필요한 wrapper 를 제거 한다.
  • The simpler the code, the faster it runs, it really is that simple.
  • string concatenation
    • echo ''; 보다 echo ''; 가 일반적이다.

  • include nested-looping 사용에 대한 주의
main
require_once
  php_self
require_once (2x)
  session_is_registered
  require_once
require_once
  require_once (3x)
    require_once (2x)
  require_once
    require_once
      require_once
        require_once
          is_array
          use_plugin
            file_exists
            include_once
            function_exists
            ... etc
As you can see, a require_once was performed and inside that a php_self was executed. Then another require_once executed session_is_registered, followed by another require_once and so on. Basically, the function call tree is like a little window into the Zend Engine, allowing you to watch the sequence of events that take place when your Web application is run.

If you do much object-oriented development, you may find that you rapidly lose track of what's actually going on inside some classes and methods. It's tempting to think of classes as a black box, because that's how we're taught to use them. But, when optimizing a complex Web application, you need to know what's actually going on inside each one, or you may have performance bottlenecks you do not even notice.

  • Avoid repeated function calls
    • for ( $i=0; $i<count($aArray); $i++ ) {} : bad
    • $nCnt = count($aArray); for ( $i=0; $i<$nCnt; $i++ ) {} : good
  • string concatenation
    • sprintf 보다 plain string concatenation 이 두배 정도 빠르다.
  • print() 대신 echo 를 사용 하라.
  • string 표현은 signle quotes 를 사용 하라.
  • Reduces Number Of System Calls (Optimizes PHP<->OS Communication)


신고

'ITWeb > 개발일반' 카테고리의 다른 글

javascript programming conventions  (0) 2007.08.23
[PHP]tutorials.  (0) 2007.08.22
PHP Performance  (0) 2007.08.22
PHP 보안 (Essential PHP Security)  (0) 2007.08.22
PHP include vs require  (0) 2007.08.22
[강좌]browser detecting  (0) 2007.04.18
Trackback 0 : Comment 0