31
PHP Performance laruence [email protected] http://www.laruence.com/ 14-May-2011

Php performance

Embed Size (px)

DESCRIPTION

PPT for PHP coding optimization and LAMP optimization

Citation preview

Page 1: Php performance

PHP [email protected]

http://www.laruence.com/14-May-2011

Page 2: Php performance

Goals

• Reduce file IO

• Less request

• Save bandwidth

• Make PHP faster

Page 3: Php performance

Compiler Optimization

• Compile PHP with –O3 flag

Page 4: Php performance

WebServer

• Apache

• Nginx

• Lighttpd

Page 5: Php performance

Apache Optimizations

• DirectoryIndex

• AllowOverride

• Options FollowSymLinks

• Deny Allow

• HostnameLookups

• Keepalive

• Log

Page 6: Php performance

Output Optimizations

• Output

• ob_start

• output_buffer

• sendBufferSize

Page 7: Php performance

Reduce Request

• Use Css instead of style

• Minifiy Javascript/Css

• Merge Javascript/Css

• Css sprite

Page 8: Php performance

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

PHP Parse Cycle

• compile_file

• execute

Page 10: Php performance

Merge Include

• Reduce Opcode

• Reduce File IO

Page 11: Php performance

Opcode Cache

• apc

• eaccelerator

Page 12: Php performance

Coding Optimization

Page 13: Php performance

Prefer Use Static Methods

<?phpclass Test {

public function a() { }

public static function b() { } }?>

• 1:4

Page 14: Php performance

Avoid Magic

• __set

• __get

• __call

Page 15: Php performance

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

Use Include instead of Include_once

• 1 hash lookup

Page 17: Php performance

@ is evil

@func()

$old = ini_set(“error_reporting”, 0);func();ini_set(“error_reporting”, $old);

Page 18: Php performance

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

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

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

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

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

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

Shorten names

• $product_car_price_in_doller

• Function getTheUserFamilyAdress

• Class PersonWhoHaveGun

Page 25: Php performance

Use PHP’s functions

• Internal Functions

• Pcel Extensions

• Pear

Page 26: Php performance

Any Other ideas?

Page 27: Php performance

Execute Method

• Call

• Swith

• Goto

Page 28: Php performance

Contents Cache

• File

• Session

• Memecache

• Expire time

Page 29: Php performance

Use PHP Extension

• C

• Avoid Compile

• Avoid Zend VM

Page 30: Php performance

Profiling & Benchmarking

• WebServer ab http_load

• PHP apd xdebug

• Mysql explain profile

Page 31: Php performance

Q&A