+ All Categories
Home > Software > PHP Optimization

PHP Optimization

Date post: 18-Jun-2015
Category:
Upload: djesch
View: 1,470 times
Download: 1 times
Share this document with a friend
Description:
PHP Optimization can increase a client’s site performance and leverage your plugin design and capacity. Writing your code with PHP in mind first will help you build stable scalable plugins and applications. It will also save you time and energy after product release. Practical tips will be shared in this session for you to implement in projects immediately. Yes, there will be code.
Popular Tags:
28
PHP Optimization by Dave Jesch - SpectrOMTech.com OC WordCamp - June 7, 2014 How to make your code run faster
Transcript
Page 1: PHP Optimization

PHP Optimization

by Dave Jesch - SpectrOMTech.comOC WordCamp - June 7, 2014

How to make your code run faster

Page 2: PHP Optimization

There will be code.

Warning:

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 3: PHP Optimization

Optimization

Dave Jesch, Lifetime Geek

➔ 1.0 Web Applications

➔ Sales Conversion Plugins

➔ eCommerce Solutions

Security

Principal, SpectrOMTech.com

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 4: PHP Optimization

Mastering Security & Optimization...

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 5: PHP Optimization

1. How to Measure Improvement2. Basic Optimization Techniques3. Code Examples

Today’s Goals:

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 6: PHP Optimization

Secret Behind Optimization

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 7: PHP Optimization

1. Ways to Measure Outcome

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 8: PHP Optimization

What are we Measuring?

● Speed

● Memory

● Code simplification / maintenance

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 9: PHP Optimization

Number of Operations

O(n) notation is used to express the worst case order of growth of an algorithm.

How an algorithm’s worst-case performance changes as the size of the data set it operates on increases.

http://www.perlmonks.org/?node_id=227909

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 10: PHP Optimization

microtime()http://us1.php.net/manual/en/function.microtime.php

memory_get_usage()http://us1.php.net/manual/en/function.memory-get-usage.php

XDebug Profilerhttp://www.xdebug.org/docs/profiler

Measuring Tools

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 11: PHP Optimization

2. Let’s make your code Awesome!

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 12: PHP Optimization

General Optimizations (1 of 7)● Upgrade!

● 'string' is not the same as "string".[2.62 : 3.78 @10,000,000]

● Use built-in functions, especially for arrays and strings. array_column()/array_replace()/implode()/explode(), etc.

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 13: PHP Optimization

General Optimizations (2 of 7)

● Identity 1===1 vs. Equality '1' == 1[1.63 : 3.72 @10,000,000]

● Shift (n >> 1), not divide (n / 2)[2.34 : 2.75 @10,000,000]

● Don’t make needless copies.

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 14: PHP Optimization

General Optimizations (3 of 7)

● Use reference operator $x = &$array['key'][1.95 : 2.01 @10,000,000]

● unset large arrays when no longer needed.

● echo $a,$b; faster than echo $a.$b;[1.11 : 2.48 @1,000,000]

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 15: PHP Optimization

General Optimizations (4 of 7)

● Avoid @ error control operator.

● Pre increment ++$i faster than post increment $i++[0.95 : 1.05 @10,000,000]

● "foreach" for arrays is better than "for"[1.38 : 4.35 @100,000]

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 16: PHP Optimization

General Optimizations (5 of 7)

● Initialize! using undefined variables is slower than pre-initialized variables.[0.82 : 4.41 @10,000,000]

● Don’t global unless you use it.[1.82 : 2.01 @10,000,000]

● switch is faster than if (…) elseif (…) ...

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 17: PHP Optimization

General Optimizations (6 of 7)

● !isset($foo{5}) faster than strlen($foo) < 5[0.26 : 0.84 @10,000,000]

● str_replace() faster than preg_replace()strtr() faster than str_replace()[0.47 : 0.49 : 1.05 @100,000]

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 18: PHP Optimization

General Optimizations (7 of 7)

● "{$var}" is faster than sprintf()[2.19 : 6.87 @10,000,000]

● incrementing global var slightly slower than a local var[8.98 : 9.09 @100,000,000]

● use ctype_alnum(), ctype_alpha() and ctype_digit() over regex

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 19: PHP Optimization

3. Examples:

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 20: PHP Optimization

Bubble Sort Optimized: O(n^2) Best case: O(n)$len = count($arr);for ($i = 0; $i < $len; ++$i) { $swapped = false; for ($j = 0; $j < $len - 1; ++$j) { if ($arr[$i] < $arr[$j]) { $x = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $x; $swapped = true; } } if (!$swapped) break;}

Non-Optimized: O(n^2)for ($i = 0; $i < count($arr); $i++) { for ($j = 0; $j < count($arr) - 1; $j++) { if ($arr[$i] < $arr[$j]) { $x = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $x; } }}

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 21: PHP Optimization

Reuse Function Results Non-Optimized:if (get_option('some_setting') > 0) $var = get_option('some_setting');else $var = 0;

Optimized:

// use option or set to 0 if option not set

if (($var = get_option('some_setting')) < 0)

$var = 0;

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 22: PHP Optimization

Iterating Arrays

Non-Optimized:// iterate through an arrayforeach ($aHash as $key => $val) {

call_func($aHash[$key]);}

12.01 seconds @10,000,000

Optimized:// iterate through an array

while (list($key) = each($aHash)) {

call_func($aHash[$key]);

}

0.73 seconds @10,000,000

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 23: PHP Optimization

For LoopsNon-Optimized:// loop through all elements of arrayfor ($i = 0; $i < count($a); ++$i) { // do something}

Optimized:

// loop through all elements of array

for ($i = 0, $c = count($a); $i < $c; ++$i) {

// do something

}© 2014 SpectrOMTech.com. All Rights Reserved.

Page 24: PHP Optimization

Latency Solutions: Transients

// get data from transientif (($value = get_transient('my_transient_name')) === false) { // nothing in transient, get from remote API $value = wp_remote_get('http://some-domain.com', $args); // save data from API in transient set_transient('my_transient_name', $value, $expiration);}

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 25: PHP Optimization

Ordering of Conditions

if (fastest() || fast() || slowest()) { // some code here}________________________________________________________________

if (op1() && op2() && op3()) { // more code}

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 26: PHP Optimization

Code Optimized!

1. Practice, practice, practice2. Always more to learn3. Balancing act4. Art form meets science

© 2014 SpectrOMTech.com. All Rights Reserved.

Page 27: PHP Optimization

WP Geek Lab- Give back to WordPress.org

Alone we can do so little; together we can do so much. - Helen Keller

Connect with us at [email protected] for more info.

write code...fix bugs...hangout...talk geek...

Page 28: PHP Optimization

More References

http://phplens.com/lens/php-book/optimizing-debugging-php.php

http://alexatnet.com/articles/php-micro-optimization-tips

http://www.mdproductions.ca/guides/50-best-practices-to-optimize-php-code-performance

http://php100.wordpress.com/2009/06/26/php-performance-google/

© 2014 SpectrOMTech.com. All Rights Reserved.


Recommended