缩小 PHP 有什么意义吗?
我知道您可以缩小 PHP,但我想知道是否有任何意义。 PHP 是一种解释语言,因此运行速度比编译语言慢一些。我的问题是:如果我缩小 PHP,客户会看到页面加载速度明显提高吗?
另外,有没有办法编译PHP或类似的东西?
I know you can minify 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?
Also, is there a way to compile PHP or something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
PHP 被编译成字节码,然后在类似于虚拟机的东西上进行解释。许多其他脚本语言都遵循相同的一般过程,包括 Perl 和 Ruby。它并不是真正的传统解释语言,例如 BASIC。
如果您尝试“缩小”源,则不会有效提高速度。通过使用像 APC 这样的字节码缓存,您将获得显着的提升。
Facebook 推出了一个名为 HipHop 的编译器,可将 PHP 源代码转换为 C++ 代码。 Rasmus Lerdorf,PHP 大佬之一今年早些时候为 Digg 做了一次演示,介绍了性能HipHop 提供的改进。简而言之,它并不比优化代码和使用字节码缓存快太多。 HipHop 对于大多数用户来说是大材小用。
Facebook 最近还推出了 HHVM,这是一个基于他们制作 HipHop 作品的新虚拟机。它仍然相当新,尚不清楚它是否会给公众带来重大的性能提升。
为了确保明确说明,请完整阅读该演示文稿。它指出了使用 xdebug 和 xhprof,也来自 Facebook。
2021 更新
HHVM 在几个版本前就脱离了普通 PHP。 PHP 7 和 8 带来了一系列令人惊叹的性能改进,几乎缩小了差距。您现在不再需要做奇怪的事情来获得更好的 PHP 性能!
由于性能原因,缩小 PHP 源代码仍然毫无用处。
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.
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.
Facebook introduced a compiler named HipHop that transforms PHP source into C++ code. Rasmus Lerdorf, one of the big PHP guys did a presentation for Digg earlier this year that 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 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.
Just to make sure it's stated expressly, please read that presentation in full. It points out numerous ways to benchmark and profile code and identify bottlenecks using tools like xdebug and xhprof, also from Facebook.
2021 Update
HHVM diverged away from vanilla PHP a couple versions ago. PHP 7 and 8 bring a whole bunch of amazing performance improvements that have pretty much closed the gap. You now no longer need to do weird things to get better performance out of PHP!
Minifying PHP source code continues to be useless for performance reasons.
放弃缩小 PHP 的想法,转而使用操作码缓存,例如
PHP 加速器
< /a> 或APC
。或者其他类似
memcached
Forgo the idea of minifying PHP in favor of using an opcode cache, like
PHP Accelerator
, orAPC
.Or something else like
memcached
是的,有一点(非技术性的)。
您的托管服务商可以在他的服务器上监视您的代码。如果你缩小和丑化它,间谍就更难窃取你的想法。
缩小和丑化 php 的原因之一可能是间谍保护。我认为丑陋的代码应该是自动部署的一步。
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 -w 可以显着压缩它,为脚本启动增加一点速度。然而,在启用操作码的服务器上,这仅保存一些文件实时检查。
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 扩展,它将 Zend 操作码转换为在具有静态类型的 VM 上运行。它不会加速任意 PHP 代码。它确实允许您编写比常规 PHP 运行速度更快的代码。这里的关键是静态类型。在现代 CPU 上,动态语言会左右分支错误预测损失。 PHP 数组是哈希表的事实也带来了很高的成本:大量分支预测错误、缓存使用效率低下、内存预取不佳以及没有任何 SIMD 优化。分支预测错误和高速缓存未命中尤其是当今处理器的致命弱点。我的小虚拟机通过使用静态类型和 C 数组而不是哈希表来回避这些问题。结果最终运行速度提高了大约十倍。这是使用字节码解释。该扩展可以选择通过 gcc 编译函数。在这种情况下,您的速度会提高两到五倍。
以下是任何感兴趣的人的链接:
https://github.com/chung-leong/qb/wiki< /a>
再次强调,该扩展不是一般的 PHP 加速器。您必须为其编写特定的代码。
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.
Here's the link for anyone interested:
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 编译器...请参阅上一个问题获取列表;但是(除非您的规模与 Facebook 一样大,或者您的应用程序的目标是在客户端运行),它们通常带来的麻烦比其价值要大得多。
简单的操作码缓存将为您带来更多的收益。或者分析您的代码以识别瓶颈,然后对其进行优化。
There are PHP compilers... see this previous question for 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
Simple opcode caching will give you more benefit for the effort involved. Or profile your code to identify the bottlenecks, and then optimise it.
您不需要缩小 PHP。
为了获得更好的性能,安装Opcode缓存;但理想的解决方案是将 PHP 升级到 5.5 版本或更高版本,因为较新版本默认有一个名为 Zend Optimiser 的操作码缓存,它比其他版本的性能更好 http://massivescale.blogspot.com/2013/06/php-55-zend-optimiser- opcache-vs-xcache.html。
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.
“重点”是使文件更小,因为较小的文件比较大的文件加载速度更快。此外,删除空格将使解析速度更快一些,因为不需要解析这些字符。
会引人注目吗?几乎从来没有,除非文件很大并且大小差异很大。
The "point" is to make the file smaller, because smaller files load faster than bigger files. Also, removing whitespace will make parsing a tiny bit faster since those characters don't need to be parsed out.
Will it be noticeable? Almost never, unless the file is huge and there's a big difference in size.