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?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Sourav
  • 17,065
  • 35
  • 101
  • 159

2 Answers2

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.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 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 '11 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 *( https://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 ?)* – Pascal MARTIN Apr 10 '11 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 '11 at 16:14
  • it would be better if you just give an simple example on how to use APC – Sourav Apr 10 '11 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. – Pascal MARTIN Apr 10 '11 at 16:21
  • PascalMARTIN I have a server hosted on GoDaddy with CentOs installed and configured for my application.How can I install APC on it?Is it inbuilt? or I need to install it on the server as on my local machine?Also,any stats of how much does it actually affect the execution for procedural PHP ? Thank you!! – KillABug Jun 08 '13 at 11:23
  • As an aside, you don't necessarily have to be running Apache to use APC, our production servers are running APC/Nginx/PHP-FPM just swimmingly :) – siliconrockstar Dec 29 '13 at 20:06
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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    plz explain a little more, and it would be better if you can give some PHP code ! – Sourav Apr 10 '11 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. – Your Common Sense Apr 10 '11 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 '11 at 16:31