Php performance
-
Upload
xinchen-hui -
Category
Technology
-
view
4.150 -
download
0
description
Transcript of Php performance
![Page 2: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/2.jpg)
Goals
• Reduce file IO
• Less request
• Save bandwidth
• Make PHP faster
![Page 3: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/3.jpg)
Compiler Optimization
• Compile PHP with –O3 flag
![Page 4: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/4.jpg)
WebServer
• Apache
• Nginx
• Lighttpd
![Page 5: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/5.jpg)
Apache Optimizations
• DirectoryIndex
• AllowOverride
• Options FollowSymLinks
• Deny Allow
• HostnameLookups
• Keepalive
• Log
![Page 6: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/6.jpg)
Output Optimizations
• Output
• ob_start
• output_buffer
• sendBufferSize
![Page 7: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/7.jpg)
Reduce Request
• Use Css instead of style
• Minifiy Javascript/Css
• Merge Javascript/Css
• Css sprite
![Page 8: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/8.jpg)
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
![Page 9: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/9.jpg)
PHP Parse Cycle
• compile_file
• execute
![Page 10: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/10.jpg)
Merge Include
• Reduce Opcode
• Reduce File IO
![Page 11: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/11.jpg)
Opcode Cache
• apc
• eaccelerator
![Page 12: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/12.jpg)
Coding Optimization
![Page 13: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/13.jpg)
Prefer Use Static Methods
<?phpclass Test {
public function a() { }
public static function b() { } }?>
• 1:4
![Page 14: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/14.jpg)
Avoid Magic
• __set
• __get
• __call
![Page 15: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/15.jpg)
Avoid Function calls
• time() - $_SERVER[REQUEST_TIME]
• phpversion() - PHP_VERSION
• get_class() - __CLASS__
• is_null() - NULL ===
• strlen($str) > 5 – isset($str{5})
• print() - echo
![Page 16: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/16.jpg)
Use Include instead of Include_once
• 1 hash lookup
![Page 17: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/17.jpg)
@ is evil
@func()
$old = ini_set(“error_reporting”, 0);func();ini_set(“error_reporting”, $old);
![Page 18: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/18.jpg)
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
![Page 19: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/19.jpg)
PCRE is slow
<?phppreg_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") ?>
![Page 20: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/20.jpg)
Do not mis-use Constants
<?php $array = array(“foo” => “bar”); echo $array[foo]?>
•Try constant•E_NOTICE•Create Tmp String•1:7.5
![Page 21: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/21.jpg)
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++) {}
![Page 22: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/22.jpg)
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;?>
![Page 23: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/23.jpg)
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() {} }
![Page 24: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/24.jpg)
Shorten names
• $product_car_price_in_doller
• Function getTheUserFamilyAdress
• Class PersonWhoHaveGun
![Page 25: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/25.jpg)
Use PHP’s functions
• Internal Functions
• Pcel Extensions
• Pear
![Page 26: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/26.jpg)
Any Other ideas?
![Page 27: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/27.jpg)
Execute Method
• Call
• Swith
• Goto
![Page 28: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/28.jpg)
Contents Cache
• File
• Session
• Memecache
• Expire time
![Page 29: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/29.jpg)
Use PHP Extension
• C
• Avoid Compile
• Avoid Zend VM
![Page 30: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/30.jpg)
Profiling & Benchmarking
• WebServer ab http_load
• PHP apd xdebug
• Mysql explain profile
![Page 31: Php performance](https://reader036.fdocuments.in/reader036/viewer/2022062513/5551722bb4c905723b8b4d84/html5/thumbnails/31.jpg)
Q&A