为什么要编译 PHP 文件?
我的一个朋友最近告诉我“为了优化你的网站,你可能需要编译你的 php 文件” 我当时想“什么?”
老实说,我从来没有听说过,我是一个“高级天真的”程序员,这意味着我是自学的,我构建了复杂的网站,但我仍然缺少一些东西......
底线:
编译 php 是什么意思?将它们转换为exe文件?为什么?更快吗?
A friend of mine told me recently "to optimize your site you may want compile your php files"
and i was like "what?"
I honestly i never heard of that, i'm a "advanced-naive" programmer, that means i'm self taught, i built complex sites but i'm still missing something...
Bottom line:
what does it mean compile php? convert them in exe files? why? is faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然 PHP 代码需要在每次调用时进行解释,但字节码是几乎可以立即运行的预编译代码。
大多数情况下,只有当您运行较大的网站时,您才会真正需要它。
以下工具可用于编译脚本或运行已编译的脚本:
While PHP code needs to be interpreted on every call, bytecode is precompiled code that runs almost instantly.
Mostly you will only really need it, if you are running a larger website.
The following tools can be used to compile scripts or run compiled scripts:
除非您追求严格的性能,否则使用 Facebook 的 HipHop 之类的东西编译 PHP 可能有点过分了。
我只需在您的计算机上安装/配置替代 PHP 缓存 (APC),它将缓存已编译的字节码,并为您带来即时性能提升。
Unless you're after serious performance then compiling PHP using something like Facebook's HipHop is probably a bit excessive.
I'd just install/configure Alternative PHP Cache (APC) on your machine which will cache the compiled bytecode and should give you an instant performance boost.
Facebook 使用这样的东西。他们的产品是 hiphop,而且是免费的。
Facebook use such things. Their product is hiphop, and it's free.
编译器的想法是将人类可读代码(C、PHP、Java 等)转换为机器可读代码。当您执行 PHP 脚本时,它们会被解释(几乎是内联编译),这意味着它们会被逐行读取,并相应地执行代码。
编译代码,意味着它是在源代码处编译的,因此已经是机器语言(或虚拟机语言(如 Java)的字节代码),因此服务器不必每次都解释代码。这使得它更快。
Facebook 创建了一个 PHP 编译器来加速他们的网站。编译代码的想法是,通常,一旦编写完成,它就会在一段时间内不会改变,因此每次执行代码时都必须将其解释为机器语言,从而产生开销。这就是为什么你的朋友说优化。
因此,它将被转换为机器语言或字节码(不是 exe,但实际上是相同的概念)。
The idea of a compiler, is to convert human readble code (C, PHP, Java etc), into machine readable code. When you execute your PHP scripts, they are interpreted (almost inline compilation), which means they are read a line by line, and the code is executed accordingly.
Compiled code, means that it is compiled at source, therefore is already in machine language (or byte code for VM languages like Java), and therefore, the server does not have to interpret the code each time. This makes it quicker.
Facebook created a PHP compiler to speed up their site. The idea of compiled code is that usually, once it is written, it doesn't change for a while, so there is an overhead in having to interpret it into machine language each time the code is executed. That is why your friend means by optimize.
It will therefore be converted into machine language or bytecode (not exe, but effecively the same concept).
您的应用程序越大,这就越有意义。 PHP 将整个程序加载到内存中,然后动态编译它:这意味着需要使用它。因此,如果您预编译它应该跳过该步骤。 Facebook 就做了类似的事情。他们通过一种称为 hip hop 的方式将他们的 php 翻译成 C++。不完全一样,但你明白了。
我怀疑这会在较小的应用程序上显示出很大的差异。
The bigger your application the more sense this makes. PHP loads your whole program into memory and then compiles it on the fly: meaning as it needs to be used. So if you pre-compile it should skip that step. Facebook does something like this. The translate their php into C++ via something called hip hop. Not exactly the same thing but you get the idea.
I doubt this will show you much difference on smaller applications.