Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Php performance

5,641 views

Published on

PPT for PHP coding optimization and LAMP optimization

Published in: Technology
  • Thank you very much! php is my favorite, and your presentation helps much! :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Php performance

  1. 1. PHP Performance laruence [email_address] http://www.laruence.com/ 14-May-2011
  2. 2. Goals <ul><li>Reduce file IO </li></ul><ul><li>Less request </li></ul><ul><li>Save bandwidth </li></ul><ul><li>Make PHP faster </li></ul>
  3. 3. Compiler Optimization <ul><li>Compile PHP with –O3 flag </li></ul>
  4. 4. WebServer <ul><li>Apache </li></ul><ul><li>Nginx </li></ul><ul><li>Lighttpd </li></ul>
  5. 5. Apache Optimizations <ul><li>DirectoryIndex </li></ul><ul><li>AllowOverride </li></ul><ul><li>Options FollowSymLinks </li></ul><ul><li>Deny Allow </li></ul><ul><li>HostnameLookups </li></ul><ul><li>Keepalive </li></ul><ul><li>Log </li></ul>
  6. 6. Output Optimizations <ul><li>Output </li></ul><ul><li>ob_start </li></ul><ul><li>output_buffer </li></ul><ul><li>sendBufferSize </li></ul>
  7. 7. Reduce Request <ul><li>Use Css instead of style </li></ul><ul><li>Minifiy Javascript/Css </li></ul><ul><li>Merge Javascript/Css </li></ul><ul><li>Css sprite </li></ul>
  8. 8. Save Bandwidth <ul><li>Freedomain Cookie </li></ul><ul><li>strip blank/comment in html </li></ul><ul><li>strip blank/comment in javascript/css </li></ul><ul><li>expire time for static content </li></ul><ul><li>compress content </li></ul><ul><ul><li>Apache mod_gizp/mod_deflate </li></ul></ul><ul><ul><li>PHP zlib.output_compress </li></ul></ul>
  9. 9. PHP Parse Cycle <ul><li>compile_file </li></ul><ul><li>execute </li></ul>
  10. 10. Merge Include <ul><li>Reduce Opcode </li></ul><ul><li>Reduce File IO </li></ul>
  11. 11. Opcode Cache <ul><li>apc </li></ul><ul><li>eaccelerator </li></ul>
  12. 12. Coding Optimization
  13. 13. Prefer Use Static Methods <ul><ul><li><?php </li></ul></ul><ul><ul><li>class Test { </li></ul></ul><ul><ul><li>public function a() { </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li> public static function b() { } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>1:4 </li></ul>
  14. 14. Avoid Magic <ul><li>__set </li></ul><ul><li>__get </li></ul><ul><li>__call </li></ul>
  15. 15. Avoid Function calls <ul><li>time() - $_SERVER[REQUEST_TIME] </li></ul><ul><li>phpversion() - PHP_VERSION </li></ul><ul><li>get_class() - __CLASS__ </li></ul><ul><li>is_null() - NULL === </li></ul><ul><li>strlen($str) == 5 – isset($str{5}) </li></ul><ul><li>print() - echo </li></ul>
  16. 16. Use Include instead of Include_once <ul><li>1 hash lookup </li></ul>
  17. 17. @ is evil <ul><li>@func() </li></ul><ul><li>$old = ini_set(“error_reporting”, 0); </li></ul><ul><li>func(); </li></ul><ul><li>ini_set(“error_reporting”, $old); </li></ul>
  18. 18. Less memory usage <ul><li>Use non-capturing Regex </li></ul><ul><ul><li>preg_replace('&quot;/(?:n|t|rn|s)+/&quot;', ' ', $origtext ); </li></ul></ul><ul><li>avoid tmp variable </li></ul><ul><ul><li>strncmp(PHP_OS, 'WIN', 3) </li></ul></ul><ul><ul><li>substr(PHP_OS, 0, 3) == 'WIN‘ </li></ul></ul><ul><li>unset variable after use </li></ul>
  19. 19. PCRE is slow <ul><li><?php </li></ul><ul><li>preg_replace( &quot;/n/&quot;, &quot;n&quot;, $text); </li></ul><ul><li>str_replace( &quot;/n/&quot;, &quot;n&quot;, $text); </li></ul><ul><li>preg_match(“/^foo_/i&quot;, &quot;FoO_&quot;) </li></ul><ul><li>!strncasecmp(&quot;foo_&quot;, &quot;FoO_&quot;, 4) </li></ul><ul><li>preg_match(“/[a8f9]/&quot;, &quot;sometext&quot;) </li></ul><ul><li>strpbrk(&quot;a8f9&quot;, &quot;sometext&quot;) </li></ul><ul><li>preg_match(&quot;!string!i&quot;, &quot;text&quot;) stripos(&quot;text&quot;, &quot;string&quot;)   </li></ul><ul><li>?> </li></ul>
  20. 20. Do not mis-use Constants <ul><li><?php </li></ul><ul><li>$array = array(“foo” => “bar”); </li></ul><ul><li>echo $array[foo] </li></ul><ul><li>?> </li></ul><ul><li>Try constant </li></ul><ul><li>E_NOTICE </li></ul><ul><li>Create Tmp String </li></ul><ul><li>1:7.5 </li></ul>
  21. 21. Do not multi-call function in for loop <ul><li><?php </li></ul><ul><li>for ($i=0; $i < count($array); $i++) {} </li></ul><ul><li>?> </li></ul><ul><li>Instead </li></ul><ul><li><?php </li></ul><ul><li>for ($i=0, $j=count($array); $i<$j; $i++) {} </li></ul>
  22. 22. Use Reference <ul><li><?php </li></ul><ul><li>$a[1][2] = array(); </li></ul><ul><li>for($i = 0; $i < 10; $i++) </li></ul><ul><li>$a[1][2][$i] = $i; </li></ul><ul><li>?> </li></ul><ul><li>Instead </li></ul><ul><li><?php </li></ul><ul><li>$ref =& $a[1][2]; </li></ul><ul><li>for($i = 0; $i < 10; $i++) </li></ul><ul><li>$ref[$i] = $i; </li></ul><ul><li>?> </li></ul>
  23. 23. Do Work for PHP <ul><li>Use full path in require/include </li></ul><ul><ul><li>Inlucde “2.php” </li></ul></ul><ul><ul><li>Include “/home/huixinchen/phpsrc/2.php” </li></ul></ul><ul><li>Less include path </li></ul><ul><li>Use instant instead of variable </li></ul><ul><li><?php </li></ul><ul><li>class test { </li></ul><ul><li>public static function instance() { return new self(); </li></ul><ul><li>} private function __construct() {} </li></ul><ul><li>} </li></ul>
  24. 24. Shorten names <ul><li>$product_car_price_in_doller </li></ul><ul><li>Function getTheUserFamilyAdress </li></ul><ul><li>Class PersonWhoHaveGun </li></ul>
  25. 25. Use PHP’s functions <ul><li>Internal Functions </li></ul><ul><li>Pcel Extensions </li></ul><ul><li>Pear </li></ul>
  26. 26. Any Other ideas?
  27. 27. Execute Method <ul><li>Call </li></ul><ul><li>Swith </li></ul><ul><li>Goto </li></ul>
  28. 28. Contents Cache <ul><li>File </li></ul><ul><li>Session </li></ul><ul><li>Memecache </li></ul><ul><li>Expire time </li></ul>
  29. 29. Use PHP Extension <ul><li>C </li></ul><ul><li>Avoid Compile </li></ul><ul><li>Avoid Zend VM </li></ul>
  30. 30. Profiling & Benchmarking <ul><li>WebServer </li></ul><ul><ul><li>ab </li></ul></ul><ul><ul><li>http_load </li></ul></ul><ul><li>PHP </li></ul><ul><ul><li>apd </li></ul></ul><ul><ul><li>xdebug </li></ul></ul><ul><li>Mysql </li></ul><ul><ul><li>explain </li></ul></ul><ul><ul><li>profile </li></ul></ul>
  31. 31. Q&A

×