什么是字节码缓存?如何在PHP中使用字节码缓存? [英] What is a bytecode cache and how can I use one in PHP?

查看:172
本文介绍了什么是字节码缓存?如何在PHP中使用字节码缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索后发现,可以对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代码编译为操作码
  • 然后:执行这些 opcode .
  • First: the PHP code, written in plain-text, is compiled to opcodes
  • Then: those opcodes are executed.


当您拥有一个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-备用PHP缓存:


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

一旦正确安装并配置了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中使用字节码缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆