1 / 31

PHP Performance

PHP Performance. laruence me@laruence.com http://www.laruence.com/ 14-May-2011. Goals. Reduce file IO Less request Save bandwidth Make PHP faster. Compiler Optimization. Compile PHP with –O3 flag. WebServer. Apache Nginx Lighttpd. Apache Optimizations. DirectoryIndex

meghan
Download Presentation

PHP Performance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Performance laruence me@laruence.com http://www.laruence.com/ 14-May-2011

  2. Goals • Reduce file IO • Less request • Save bandwidth • Make PHP faster

  3. Compiler Optimization • Compile PHP with –O3 flag

  4. WebServer • Apache • Nginx • Lighttpd

  5. Apache Optimizations • DirectoryIndex • AllowOverride • Options FollowSymLinks • Deny Allow • HostnameLookups • Keepalive • Log

  6. Output Optimizations • Output • ob_start • output_buffer • sendBufferSize

  7. Reduce Request • Use Css instead of style • Minifiy Javascript/Css • Merge Javascript/Css • Css sprite

  8. Save Bandwidth • Freedomain Cookie • strip blank/comment in html • strip blank/comment in javascript/css • expire time for static content • compress content • Apache mod_gizp/mod_deflate • PHP zlib.output_compress

  9. PHP Parse Cycle • compile_file • execute

  10. Merge Include • Reduce Opcode • Reduce File IO

  11. Opcode Cache • apc • eaccelerator

  12. Coding Optimization

  13. Prefer Use Static Methods <?php class Test { public function a() { } public static function b() { } } ?> • 1:4

  14. Avoid Magic • __set • __get • __call

  15. Avoid Function calls • time() - $_SERVER[REQUEST_TIME] • phpversion() - PHP_VERSION • get_class() - __CLASS__ • is_null() - NULL === • strlen($str) > 5 – isset($str{5}) • print() - echo

  16. Use Include instead of Include_once • 1 hash lookup

  17. @ is evil @func() $old = ini_set(“error_reporting”, 0); func(); ini_set(“error_reporting”, $old);

  18. Less memory usage • Use non-capturing Regex • preg_replace('"/(?:\n|\t|\r\n|\s)+/"', ' ', $origtext ); • avoid tmp variable • strncmp(PHP_OS, 'WIN', 3) • substr(PHP_OS, 0, 3) == 'WIN‘ • unset variable after use

  19. PCRE is slow <?php preg_replace( "/\n/", "\\n", $text); str_replace( "/\n/", "\\n", $text); preg_match(“/^foo_/i", "FoO_") !strncasecmp("foo_", "FoO_", 4) preg_match(“/[abce]/", “string") strpbrk(“string", “abcd") preg_match("!string!i", "text")stripos("text", "string") ?>

  20. Do not mis-use Constants <?php $array = array(“foo” => “bar”); echo $array[foo] ?> • Try constant • E_NOTICE • Create Tmp String • 1:7.5

  21. Do not multi-call function in for loop <?php for ($i=0; $i < count($array); $i++) {} ?> • Instead <?php for ($i=0, $j=count($array); $i<$j; $i++) {}

  22. Use Reference <?php $a[1][2] = array(); for($i = 0; $i < 10; $i++) $a[1][2][$i] = $i; ?> • Instead <?php $ref =& $a[1][2]; for($i = 0; $i < 10; $i++) $ref[$i] = $i; ?>

  23. Do Work for PHP • Use full path in require/include • Inlucde “2.php” • Include “/home/huixinchen/phpsrc/2.php” • Less include path • Use instant instead of variable <?php class test { public static function instance() { return new self(); } private function __construct() {} }

  24. Shorten names • $product_car_price_in_doller • Function getTheUserFamilyAdress • Class PersonWhoHaveGun

  25. Use PHP’s functions • Internal Functions • Pcel Extensions • Pear

  26. Any Other ideas?

  27. Execute Method • Call • Swith • Goto

  28. Contents Cache • File • Session • Memecache • Expire time

  29. Use PHP Extension • C • Avoid Compile • Avoid Zend VM

  30. Profiling & Benchmarking • WebServer • ab • http_load • PHP • apd • xdebug • Mysql • explain • profile

  31. Q&A

More Related