问题描述
我在网上搜索并了解到可以编译 PHP 代码以提高性能.但是怎么做呢?我可以编译面向过程和面向对象的 PHP 代码吗?
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?
推荐答案
基本思路,执行PHP脚本时分两步:
The basic idea, when executing a PHP script is in two steps :
- 首先:以纯文本形式编写的 PHP 代码被编译为操作码
- 然后:执行那些操作码.
当你有一个 PHP 脚本时,只要不修改,操作码永远是一样的;因此,每次执行该脚本时都进行编译阶段是一种 CPU 时间的浪费.
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.
一旦 PHP 脚本被编译为操作码,这些操作码将保存在 RAM 中——并在下次执行脚本时直接从内存中使用;防止编译一次又一次.
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.
使用最多的操作码缓存是APC - Alternative PHP Cache :
- 在 PECL 上查看以下载 APC 扩展
- 这是手册
一旦 APC 安装并正确配置,您就无需在 PHP 代码中修改任何内容:APC 将缓存操作码,仅此而已 -- 该进程对您的应用程序完全不可见.
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.
这篇关于什么是字节码缓存,如何在 PHP 中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!