問題描述
我在網上搜索并了解到可以編譯 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 中使用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!