26

I searched on the Web and came to know that PHP code can be compiled to have performance boost. But how to do it? Can I compile both procedural and object oriented PHP code?

2 Answers 2

39

The basic idea, when executing a PHP script is in two steps :

  • First: the PHP code, written in plain-text, is compiled to opcodes
  • Then: those opcodes are executed.


When you have one PHP script, as long as it is not modified, the opcodes will always be the same ; so, doing the compilation phase each time that script is to be executed is kind of a waste of CPU-time.

To prevent that redundant-compilation, there are some opcode caching mechanism that you can use.

Once the PHP script has been compiled to opcodes, those will be kept in RAM -- and directly used from memory the next time the script is to be executed ; preventing the compilation from being done again and again.


The opcode cache which is used the most is APC - Alternative PHP Cache :

Once APC has been installed and configured properly, there is nothing you have to modify in your PHP code : APC will cache the opcodes, and that is all -- the process is totally invisible for your application.

7
  • 1
    so it is complete difference scenario than C/C++... where we write code in .c file and compile to .exe and run that exe file ! what about this case ?
    – Sourav
    Apr 10, 2011 at 16:07
  • 3
    Yes, it is a completly different idea : C/C++ are compiled languages, while PHP is more of the interpreted kind ;;; if you want to compile your PHP code to some kind of executable, you could take a look at HipHop ( github.com/facebook/hiphop-php/wiki ) -- but note that this is generally not quite necessary (I've actually never seen anyone use HipHop on a production server -- except facebook, of course... But is you website that important ?) Apr 10, 2011 at 16:09
  • nope, never like Facebook :) , but can i use APC on my PHP 13000 lines code which is written in procedural style ?
    – Sourav
    Apr 10, 2011 at 16:14
  • it would be better if you just give an simple example on how to use APC
    – Sourav
    Apr 10, 2011 at 16:14
  • 2
    Yes, you can use APC (your code doesn't matter) ; just install the extension, edit your php.ini to enable it and configure it ; restart Apache so the modification is taken into account ; et voila ; you don't have anything else to do. Apr 10, 2011 at 16:21
1

But how to do it?

Easy.
First of all you have to do some profiling to be sure that code parsing being a bottleneck of your site, and all other obvious ones like unoptimized data storage, slow algorithms, data mining and network calls were optimized.

Easiest way to determine if you need opcode cache or not would be just putting this line at the very top of your most used page

$timer_start = microtime(1);

and this line at the very end:

echo "Generated in ".(round((microtime(1) - $timer_start),4))." sec.";

if time is more than 0.01, you have other things to optimize first, because you will notice no effect from opcode cache.

3
  • 1
    plz explain a little more, and it would be better if you can give some PHP code !
    – Sourav
    Apr 10, 2011 at 16:22
  • It's a long story. In short, you have to be certain if you really need opcode. 99.99% sites in the world do not use it as it's just useless for them. Apr 10, 2011 at 16:24
  • 3
    That's a fairly bold statement. Most people don't use APC, simply because they are on shared hosting and thus can't use it. But as far as I know pretty much anybody on virtual or dedicated hosting uses APC. There are loads of things that won't improve your sites performance, but APC is one that definitely does (measurably, just Google).
    – NikiC
    Apr 10, 2011 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.