What is a bytecode cache and how can I use one in PHP?
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 :
- See on PECL to download the APC extension
- And here's its manual
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.
Related videos on Youtube
Sourav
Hi, I am Sourav Ghosh :) You can find my projects * AJAX Micro Mini Lib [JS] * Autorun Cleaner [VB.NET] * C Code Completer [VB.NET] * DockBar Develop [VB.NET] * ShoutOut-Twitter [ASP.NET + C#] * Ultra Light CAPTCHA [PHP] * Ultra Light Forum [PHP] * Wallpaper Changer [VB.NET]** @ http://sourceforge.net/users/sourav1989 I'll be really happy if you find them useful :) Thanks :)
Updated on May 28, 2022Comments
-
Sourav almost 2 years
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?
-
Sourav about 13 yearsso 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 ?
-
Pascal MARTIN about 13 yearsYes, 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 ?)
-
Sourav about 13 yearsnope, never like Facebook :) , but can i use APC on my PHP 13000 lines code which is written in procedural style ?
-
Sourav about 13 yearsit would be better if you just give an simple example on how to use APC
-
Pascal MARTIN about 13 yearsYes, 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.
-
Sourav about 13 yearsplz explain a little more, and it would be better if you can give some PHP code !
-
Your Common Sense about 13 yearsIt'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.
-
NikiC about 13 yearsThat'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).
-
KillABug almost 11 yearsPascalMARTIN 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!!
-
siliconrockstar over 10 yearsAs 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 :)