PHP optimization
based on many information found on the internet and on
personal experience
by Fabrizio Parrella
How where the tests performed
Those tests have been made and repeated multiple
times then the values have been averaged and
the outliers removed. Your script/server might
give you different results, this might be because
your CPU/memory/internal process/PHP Garbage
Collector/etc... might interfere
String Output
Is there a difference between “echo” and “print” ?
“echo” and “print” serve the same exact purpose.
Various tests show that “echo” is about 10-15%
faster when using comma separated items.
Variable Type Checking
isset() vs. empty() vs is_array()
Often we need to check if a variable is set, so what
is the faster ?
isset() and empty() are pretty much identical
is_array() takes twice as long
Quotes
“ vs. '
Lots of information on the web about quotes.. which
is faster ?
In the past there were some minor performance
differences between “ and ' for a string that
doesn't contain variables.
In today's versions of PHP looks like that the
argument has been satisfied on both sides as
they are pretty much the same
Counting Loops
for vs. while
While or For.. For or While ? Many tests have been
done and many results have been posted all over
the internet.
In reality, there is not enough difference in execution
to make any very impact in the execution of the
application.
Micro-optimizing ?
The “while” is about 13% faster on an empty loop
that counts up to 1,000,000
Read Loops
foreach vs. for vs. while(list() = each())
This test is very tested like the “Counting Loops”
and there are various results around.
In my experience and the most commonly results
from other tests is that “foreach()” without $key is
the winner. The next closest is the “for()” and the
last is the “while()”.
If you must use the “while()” do not forget to use “reset()” first to
incredibly speed up the process.
“reset()” is good to use anytime you do a loop to reposition the pointer to the beginning
Control Structures
switch/case/default vs. if/elseif/else
There is no real difference between “switch” and “if”
There is a difference is the equality sign, where ===
is about 10% faster than ==
Counting Loops
For-Loop tests – calculate size in advance
Unsurprising results, calculate the size of the loop
ahead of time for a 1000x repetitions is incredibly
faster:
test time
Pre-calc - count() 243µs
No pre-calc - count() 109,472µs
Pre-calc - sifeOf() 200µs
No pre-calc - sizeOf() 104,085µs
Modify Loop
foreach() vs. for vs. while(list() = each())
Often happens that you need to modify the array
while you are looping it, when doing so it is best
not to loop the the array while modifying it
test time
foreach($array as $k=>$v) $array[$k] = 'value'; 362µs
while(list($k) = each($array)) $array[$k] = ''value'; 165µs
$ks = array_keys($array);
$size = count($k);
for($i=0; $i<=$size; $i++) $array[$ks[$i]] = 'value';
72µs
Got Perfect Code ?
so you wrote perfect code, but you want it to go faster
Have you ever heard of OPCODE caching?
What is OPCODE caching?
When a PHP script is called by a user:
●
User Requests the Page
●
Web Server Compiles the PHP script (interprets)
●
Executes the Compiled Version of the Page
●
Output the Data
Normal Applications do not need to be compiled as they are often pre-compiled,
however scripting languages like PHP, Perl, Ruby, etc., are compiled on demand
and interpreted. (there is a difference between compiling and interpreting, but that
is for another time).
As you can imagine, converting source code into machine code (opcode) can be
resource intensive. Opcode caches will store opcode in a cache and execute is
from there instead than constantly recompiling the code. This basically eliminate
the second step and seriously speed up the site.
It is Worth ?
You know what it is.. but is it worth to spend time installing it ?
Online research showed me that websites had
improvements between 200% and 700%.
My personal experience showed me a time saving
of 120%.
For example a page that before OPCode caching
took 10 µs to load, with APC installed it took 4µs.
The Best OPCode cache
eAccellerator, APC, Xcache, ionCube, Turck MMCache, Zend, etc..
There are MANY different OPCode cache that can
be installed for PHP, some are new, and some
are well seasoned.
My favorite are the ones that use shared memory to
reduce slow disk accesses.
I have tested different ones, my current favorite is
APC with PHP 5.3
BEST OPTIMIZATION?
Done all of this but PHP is still slow ?
You can optimize your PHP code and install
OPCode cache but if you still have issues you can
check the followings:
●
Database Queries
●
Disk accesses (fopen, dir, etc.)
●
Network (DNS, open on URLs, etc.)
●
Memory usage (memory_get_usage, memory_get_peak_usage)
●
Modules Installed
●
Endless Loops, Bugs

Php optimization

  • 1.
    PHP optimization based onmany information found on the internet and on personal experience by Fabrizio Parrella
  • 2.
    How where thetests performed Those tests have been made and repeated multiple times then the values have been averaged and the outliers removed. Your script/server might give you different results, this might be because your CPU/memory/internal process/PHP Garbage Collector/etc... might interfere
  • 3.
    String Output Is therea difference between “echo” and “print” ? “echo” and “print” serve the same exact purpose. Various tests show that “echo” is about 10-15% faster when using comma separated items.
  • 4.
    Variable Type Checking isset()vs. empty() vs is_array() Often we need to check if a variable is set, so what is the faster ? isset() and empty() are pretty much identical is_array() takes twice as long
  • 5.
    Quotes “ vs. ' Lotsof information on the web about quotes.. which is faster ? In the past there were some minor performance differences between “ and ' for a string that doesn't contain variables. In today's versions of PHP looks like that the argument has been satisfied on both sides as they are pretty much the same
  • 6.
    Counting Loops for vs.while While or For.. For or While ? Many tests have been done and many results have been posted all over the internet. In reality, there is not enough difference in execution to make any very impact in the execution of the application. Micro-optimizing ? The “while” is about 13% faster on an empty loop that counts up to 1,000,000
  • 7.
    Read Loops foreach vs.for vs. while(list() = each()) This test is very tested like the “Counting Loops” and there are various results around. In my experience and the most commonly results from other tests is that “foreach()” without $key is the winner. The next closest is the “for()” and the last is the “while()”. If you must use the “while()” do not forget to use “reset()” first to incredibly speed up the process. “reset()” is good to use anytime you do a loop to reposition the pointer to the beginning
  • 8.
    Control Structures switch/case/default vs.if/elseif/else There is no real difference between “switch” and “if” There is a difference is the equality sign, where === is about 10% faster than ==
  • 9.
    Counting Loops For-Loop tests– calculate size in advance Unsurprising results, calculate the size of the loop ahead of time for a 1000x repetitions is incredibly faster: test time Pre-calc - count() 243µs No pre-calc - count() 109,472µs Pre-calc - sifeOf() 200µs No pre-calc - sizeOf() 104,085µs
  • 10.
    Modify Loop foreach() vs.for vs. while(list() = each()) Often happens that you need to modify the array while you are looping it, when doing so it is best not to loop the the array while modifying it test time foreach($array as $k=>$v) $array[$k] = 'value'; 362µs while(list($k) = each($array)) $array[$k] = ''value'; 165µs $ks = array_keys($array); $size = count($k); for($i=0; $i<=$size; $i++) $array[$ks[$i]] = 'value'; 72µs
  • 11.
    Got Perfect Code? so you wrote perfect code, but you want it to go faster Have you ever heard of OPCODE caching?
  • 12.
    What is OPCODEcaching? When a PHP script is called by a user: ● User Requests the Page ● Web Server Compiles the PHP script (interprets) ● Executes the Compiled Version of the Page ● Output the Data Normal Applications do not need to be compiled as they are often pre-compiled, however scripting languages like PHP, Perl, Ruby, etc., are compiled on demand and interpreted. (there is a difference between compiling and interpreting, but that is for another time). As you can imagine, converting source code into machine code (opcode) can be resource intensive. Opcode caches will store opcode in a cache and execute is from there instead than constantly recompiling the code. This basically eliminate the second step and seriously speed up the site.
  • 13.
    It is Worth? You know what it is.. but is it worth to spend time installing it ? Online research showed me that websites had improvements between 200% and 700%. My personal experience showed me a time saving of 120%. For example a page that before OPCode caching took 10 µs to load, with APC installed it took 4µs.
  • 14.
    The Best OPCodecache eAccellerator, APC, Xcache, ionCube, Turck MMCache, Zend, etc.. There are MANY different OPCode cache that can be installed for PHP, some are new, and some are well seasoned. My favorite are the ones that use shared memory to reduce slow disk accesses. I have tested different ones, my current favorite is APC with PHP 5.3
  • 15.
    BEST OPTIMIZATION? Done allof this but PHP is still slow ? You can optimize your PHP code and install OPCode cache but if you still have issues you can check the followings: ● Database Queries ● Disk accesses (fopen, dir, etc.) ● Network (DNS, open on URLs, etc.) ● Memory usage (memory_get_usage, memory_get_peak_usage) ● Modules Installed ● Endless Loops, Bugs