310 likes | 427 Vues
Learn PHP performance optimization tips from laruence.me. Reduce file IO, save bandwidth, and make PHP faster. Compilation and web server optimizations covered. Improve coding and memory usage efficiency for better PHP performance.
E N D
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 • AllowOverride • Options FollowSymLinks • Deny Allow • HostnameLookups • Keepalive • Log
Output Optimizations • Output • ob_start • output_buffer • sendBufferSize
Reduce Request • Use Css instead of style • Minifiy Javascript/Css • Merge Javascript/Css • Css sprite
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
PHP Parse Cycle • compile_file • execute
Merge Include • Reduce Opcode • Reduce File IO
Opcode Cache • apc • eaccelerator
Prefer Use Static Methods <?php class Test { public function a() { } public static function b() { } } ?> • 1:4
Avoid Magic • __set • __get • __call
Avoid Function calls • time() - $_SERVER[REQUEST_TIME] • phpversion() - PHP_VERSION • get_class() - __CLASS__ • is_null() - NULL === • strlen($str) > 5 – isset($str{5}) • print() - echo
Use Include instead of Include_once • 1 hash lookup
@ is evil @func() $old = ini_set(“error_reporting”, 0); func(); ini_set(“error_reporting”, $old);
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
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") ?>
Do not mis-use Constants <?php $array = array(“foo” => “bar”); echo $array[foo] ?> • Try constant • E_NOTICE • Create Tmp String • 1:7.5
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++) {}
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; ?>
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() {} }
Shorten names • $product_car_price_in_doller • Function getTheUserFamilyAdress • Class PersonWhoHaveGun
Use PHP’s functions • Internal Functions • Pcel Extensions • Pear
Execute Method • Call • Swith • Goto
Contents Cache • File • Session • Memecache • Expire time
Use PHP Extension • C • Avoid Compile • Avoid Zend VM
Profiling & Benchmarking • WebServer • ab • http_load • PHP • apd • xdebug • Mysql • explain • profile