Download presentation
Presentation is loading. Please wait.
Published byYuliana Iskandar Modified over 6 years ago
1
laruence me@laruence.com http://www.laruence.com/ 14-May-2011
PHP Performance laruence 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 PHP
WebServer OS Client
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 PHP Exprs Opcodes Output
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 avoid tmp variable unset variable after use
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 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 $ref =& $a[1][2]; $ref[$i] = $i;
23
Do Work for PHP Use full path in require/include Less include path
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.