PHP: Myths and Truths About Performance

Performance, code readability, code logic and similar are the cornerstones between which one must constantly navigate when building a larger or smaller PHP project. I'll start by saying that any measurement is relative and depends greatly on both the environment and the PHP version.

Performance, code readability, code logic and similar are the cornerstones between which one must constantly navigate when building a larger or smaller PHP project. I'll start by saying that any measurement is relative and depends greatly on both the environment and the PHP version.

For example, it is generally believed that PHP 4.4.6 is about 33% faster than PHP 5.2.2
http://barry.wordpress.com/2007/05/28/php-4-vs-php-5-basic-benchmark/

While PHP 5.2 is faster than PHP 5.1.6
http://www.improvedsource.com/view.php/Web-Performance/11/

Fortunately (or unfortunately), it is not always possible to choose the platform - but it is almost always possible to choose one function over another.

A few examples:

Associative arrays. $a["one"] vs $a[one]
No Quotes - avg 0.008120 seconds
* Quotes - avg 0.003794 seconds

preg_replace() vs. ereg_replace() vs. str_replace()
* str_replace() - avg 0.001622 seconds
preg_replace() - avg 0.003306 seconds
ereg_replace() - avg 0.002706 seconds

Static vs. Non Static Methods
* Static Method - avg 0.001358 seconds
Non-Static Method - avg 0.002631 seconds

Error Suppression vs. None
Error Suppression - avg 0.009935 seconds
* No Error Suppression - avg 0.006685 seconds

Split() vs. Explode()
split() - avg 0.002042 seconds
explode() - avg 0.002652 seconds

(!) Other sources indicate that explode is faster than split, which in my view is logical, since split uses regex for splitting. The order would most likely be: explode, preg_split and only then split.

While() vs. For() vs. Foreach()
* while - avg 0.000309 seconds
for - avg 0.000330 seconds
foreach - avg 0.000337 seconds

is_numeric() vs. ctype_digit()
ctype_digit() - avg 0.002235 seconds
* is_numeric() - avg 0.001969 seconds


Echo vs. Print
Echo - avg 0.001092 seconds
Print - avg 0.001188 seconds

Numeric vs. Associative Foreach
Numeric foreach - avg 0.001630 seconds
Associative foreach - avg 0.001663 seconds

" vs. '
' - avg 0.000893 seconds
" - avg 0.000897 seconds

=== is faster than ==

include() is faster than include_once()

++$i is faster than $i++

time() is faster than date('U')

(int) is faster than intval()

Regarding PDO_mysql vs mysql vs mysqli, opinions vary. For example, in laacz's test it emerged that the fastest is mysqli, followed closely by pdo_mysql, while information found on the Devolio blog indicates that PDO is up to 90% faster than mysql.
Either way, PDO_mysql is a worthwhile abstract layer.


Davolio - http://www.devolio.com/blog/archives/314-Practical-and-impractical-PHP-Optimizations.html

Menhir - http://blog.menhir.be/2008/09/16/speed-tests-in-php-and-javascript/

http://bitfilm.net/2007/08/24/tips-for-faster-php-scripts/
http://bitfilm.net/2007/09/07/more-tips-for-faster-php-scripts/

Laacz - http://laacz.lv/2007/06/29/db-abstrakcijas-leijeri/

 

 

Share:
Rate: 4 (10)
Views:

comments



What are others reading?