有必要缩小 PHP 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4079920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a point to minifying PHP?
提问by Bojangles
I know you canminify PHP, but I'm wondering if there is any point. PHP is an interpreted language so will run a little slower than a compiled language. My question is: would clients see a visible speed improvement in page loads and such if I were to minify my PHP?
我知道您可以缩小 PHP,但我想知道是否有任何意义。PHP 是一种解释型语言,因此运行速度会比编译型语言慢一些。我的问题是:如果我要缩小我的 PHP,客户会看到页面加载速度的明显提高吗?
Also, is there a way to compile PHP or something similar?
另外,有没有办法编译PHP或类似的东西?
回答by Charles
PHP is compiled into bytecode, which is then interpreted on top of something resembling a VM. Many other scripting languages follow the same general process, including Perl and Ruby. It's not really a traditional interpreted language like, say, BASIC.
PHP 被编译成字节码,然后在类似于 VM 的东西上进行解释。许多其他脚本语言遵循相同的一般过程,包括 Perl 和 Ruby。它并不是真正的传统解释语言,例如 BASIC。
There would be no effective speed increase if you attempted to "minify" the source. You would get a major increase by using a bytecode cache like APC.
如果您尝试“缩小”源,则不会有效提高速度。通过使用像 APC 这样的字节码缓存,你会得到很大的提升。
Facebook introduced a compiler named HipHopthat transforms PHP source into C++ code. Rasmus Lerdorf, one of the big PHP guys did a presentation for Digg earlier this yearthat covers the performance improvements given by HipHop. In short, it's not too much faster than optimizing code and using a bytecode cache. HipHop is overkill for the majority of users.
Facebook 推出了一个名为HipHop的编译器,可将 PHP 源代码转换为 C++ 代码。Rasmus Lerdorf,PHP 大佬之一,今年早些时候为 Digg做了一次演讲,介绍了 HipHop 提供的性能改进。简而言之,它并不比优化代码和使用字节码缓存快太多。HipHop 对大多数用户来说太过分了。
Facebook also recently unveiled HHVM, a new virtual machine based on their work making HipHop. It's still rather new and it's not clear if it will provide a major performance boost to the general public.
Facebook 最近还推出了HHVM,这是一款基于他们制作 HipHop 的工作的新虚拟机。它仍然相当新,目前尚不清楚它是否会为公众提供重大的性能提升。
Just to make sure it's stated expressly, please read that presentationin full. It points out numerous ways to benchmark and profile code and identify bottlenecks using tools like xdebugand xhprof, also from Facebook.
为了确保明确说明,请完整阅读该演示文稿。它指出了许多使用xdebug和xhprof等工具对代码进行基准测试和分析以及识别瓶颈的方法,这些工具也来自 Facebook。
回答by Stephen
Forgo the idea of minifying PHP in favor of using an opcode cache, like PHP Accelerator
, or APC
.
放弃缩小 PHP 的想法,转而使用操作码缓存,例如PHP Accelerator
, 或APC
。
Or something else like memcached
或者其他类似的东西 memcached
回答by mario
With some rewriting (shorter variable names) you could save a few bytes of memory, but that's also seldomly significant.
通过一些重写(更短的变量名称),您可以节省几个字节的内存,但这也很少有意义。
However I do design some of my applications in a way that allows to concatenate include scripts together. With php -w
it can be compacted significantly, adding a little speed gain for script startup. On an opcode-enabled server this however only saves a few file mtime checks.
但是,我确实以允许将包含脚本连接在一起的方式设计了一些应用程序。有了php -w
它可以显着压缩,为脚本启动增加一点速度增益。然而,在启用操作码的服务器上,这只会保存一些文件 mtime 检查。
回答by cleong
This is less an answer than an advertisement. I'm been working on a PHP extension that translates Zend opcodes to run on a VM with static typing. It doesn't accelerate arbitrary PHP code. It does allow you to write code that run way faster than what regular PHP allows. The key here is static typing. On a modern CPU, a dynamic language eats branch misprediction penalty left and right. Fact that PHP arrays are hash tables also imposes high cost: lot of branch mispredictions, inefficient use of cache, poor memory prefetching, and no SIMD optimization whatsoever. Branch misprediction and cache misses in particular are achilles' heel for today's processors. My little VM sidesteps those problem by using static types and C array instead of hash table. The result ends up running roughly ten times faster. This is using bytecode interpretation. The extension can optionally compile a function through gcc. In that case, you get two to five times more speed.
这与其说是一个广告不如说是一个答案。我一直在开发一个 PHP 扩展,它将 Zend 操作码转换为在具有静态类型的 VM 上运行。它不会加速任意 PHP 代码。它确实允许您编写比常规 PHP 允许的运行速度更快的代码。这里的关键是静态类型。在现代 CPU 上,动态语言会左右分支错误预测惩罚。PHP 数组是哈希表这一事实也带来了高成本:大量分支预测错误、缓存使用效率低下、内存预取不佳以及没有任何 SIMD 优化。分支预测错误和缓存未命中尤其是当今处理器的致命弱点。我的小虚拟机通过使用静态类型和 C 数组而不是哈希表来回避这些问题。结果最终运行速度大约快了十倍。这是使用字节码解释。扩展可以选择通过 gcc 编译一个函数。在这种情况下,您的速度会提高两到五倍。
Here's the link for anyone interested:
这是任何有兴趣的人的链接:
https://github.com/chung-leong/qb/wiki
https://github.com/chung-leong/qb/wiki
Again, the extension is not a general PHP accelerator. You have to write code specific for it.
同样,该扩展不是通用的 PHP 加速器。您必须为它编写特定的代码。
回答by firewall
You don't need to minify PHP. In order to get a better performance, install an Opcode cache; but the ideal solution would be to upgrade your PHP to the 5.5 version or above because the newer versions have an opcode cache by default called Zend Optimiser that is performing better than the other ones http://massivescale.blogspot.com/2013/06/php-55-zend-optimiser-opcache-vs-xcache.html.
您不需要缩小 PHP。为了获得更好的性能,安装一个 Opcode 缓存;但理想的解决方案是将您的 PHP 升级到 5.5 或更高版本,因为较新的版本默认有一个操作码缓存,称为 Zend Optimiser,其性能优于其他版本http://massivescale.blogspot.com/2013/06 /php-55-zend-optimiser-opcache-vs-xcache.html。
回答by Dieter Porth
Yes there is one (non-technical) point.
是的,有一点(非技术性的)。
Your hoster can spy your code on his server. If you minify and uglify it, it is for spys more difficult to steal your ideas.
您的托管商可以在他的服务器上监视您的代码。如果你缩小和丑化它,间谍就更难窃取你的想法。
One reason for minifying and uglifying php may be spy-protection. I think uglyfing code should one step in an automatic deployment.
缩小和丑化 php 的原因之一可能是间谍保护。我认为丑化代码应该是自动部署的一步。
回答by Mark Baker
There are PHP compilers... see this previous questionfor a list; but (unless you're the size of Facebook or are targetting your application to run client-side) they're generally a lot more trouble than they're worth
有 PHP 编译器……请参阅上一个问题以获取列表;但是(除非你的规模和 Facebook 一样大或者你的应用程序的目标是在客户端运行)它们通常比它们的价值要麻烦得多
Simple opcode caching will give you more benefit for the effort involved. Or profile your code to identify the bottlenecks, and then optimise it.
简单的操作码缓存将为您带来更多好处。或者分析您的代码以识别瓶颈,然后对其进行优化。