php performance

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

Upload: meghan

Post on 13-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

PHP Performance. laruence [email protected] 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 - PowerPoint PPT Presentation

TRANSCRIPT

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