在HHVM上,用Hack编写的代码比用PHP编写的代码快吗? [英] Is code written in Hack faster than code written in PHP on HHVM?

查看:116
本文介绍了在HHVM上,用Hack编写的代码比用PHP编写的代码快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在HHVM上从PHP过渡到Hack,我们可以期望速度提高吗?

Can we expect a speed gain by transitioning from PHP to Hack on HHVM?

我正在考虑强类型化的参数/返回类型,尤其是标量,这样做是否允许HHVM在将代码编译为本地代码方面做得更好,或者与使用经典PHP和它的混合类型?

I'm thinking of the strongly typed parameters / return types, in particular scalars, does that allow HHVM to do a better job at compiling the code to native code, or is the speed gain insignificant as compared to using classic PHP and its mixed types?

推荐答案

我在Reddit上回答了此问题几个月前.我在下面复制了我的答案,因为从那以后世界的状况并没有太大改变.但是请记住,HHVM实际上仍在发展,实际上发展很快,因此这很容易在一个月或两个月后就过时了.

I answered this on Reddit a few months back. I've copied my answer below, since the state of the world hasn't changed much since then. But keep in mind that HHVM is still evolving, pretty quickly actually, and so this could easily be out of date in a month or two more.

我在Facebook的Hack团队工作.这个问题的答案有些微妙.

I work on the Hack team at Facebook. The answer to this question is somewhat subtle.

正如其他人所说,将您的PHP代码从PHP5迁移到HHVM可能会显着提高速度.重要程度取决于大量因素.如果您已经受到IO的约束,那么您可能根本看不到任何东西.如果您接近CPU极限,则据报道速度可提高5倍左右,尽管您可能会在两者之间找到一些东西.您应该使用真正的工作负载在自己的代码上尝试-HHVM有很多因素,尤其是较大的启动时间,这使其在微基准测试中表现不佳,但在实际工作负载下,它的性能应超过PHP5.为了获得最大利益,重构代码以使事情脱离顶层并进入函数/类将大有帮助(我们无法在顶层JIT代码),就像设置

Moving your PHP code from PHP5 to HHVM is likely to result in a significant speedup, as others have said. How significant depends on a ton of factors. If you're already IO bound, you may not see much at all; if you are closer to CPU bound, speedups of up to something like 5x have been reported, though you are likely to get something somewhere inbetween. You should go and try it on your own code, with a real workload -- HHVM has a bunch of factors, notably a larger startup time, that make it not do so well on microbenchmarks, but on real workloads it should outperform PHP5. For maximum benefit, refactoring your code to get things out of toplevel and into functions/classes will help a ton (we can't JIT code at toplevel), as will setting up repo authoritative mode.

但这只是HHVM上的纯PHP,而不是HHVM上的Hack.从将代码转换为Hack到什么速度?这取决于您如何进行转换,但是答案至少在目前是不是很多".如果仅将<?hh粘贴在每个文件的顶部,而不是<?php,并修复

But that's just plain PHP on HHVM, not Hack on HHVM. What speedup do you get from then converting your code to Hack? It depends on how you do the conversion, but the answer is, at least right now, "not very much". If you just go stick <?hh at the top of every file, instead of <?php, and fix any incompatibilities that come up when you run the typechecker, then your code will very likely perform the same as before. Hack and PHP code have the same runtime representation, so you haven't really changed much.

但是,如果您这样做,那么您将无法充分利用Hack!如果您开始添加类型注释,则可以为HHVM建立越来越多的信息,以便在运行时使用. 这个过程可以加快代码的速度-HHVM可以在很多情况下生成以前无法进行类型推断的特定类型的代码(即,速度更快)的代码.也不要指望在这里有巨大的提速-这在很大程度上只是理论上的事情,而且在很多地方我们可以更好地利用类型信息来生成更快的代码. (例如,我们目前在运行时对返回类型的处理不多.)但这部分可能会有所帮助,并且随着HHVM变得越来越聪明,可能会有所帮助.

If you do that, though, then you aren't taking full advantage of Hack! If you go in and start adding type annotations, you can build up more and more information for HHVM to use at runtime. This process is what can speed up your code -- HHVM can generate type-specialized (i.e., faster) code in a lot of circumstances where before it might have not been able to infer the type. Don't expect a huge speedup here either -- this is largely theoretical right now, and there are lots of places we can be taking better advantage of the type information to generate faster code. (We don't do much with return types at runtime right now, for example.) But this is the part that might help, and might help more as HHVM gets smarter.

但是,当然要记住,执行速度并不是Hack的重点-它关系到开发人员的效率.从HHVM上的PHP到HHVM上的Hack所做的任何性能提升可能都不值得.不过,开发人员生产力的提高可能是.

But of course, keep in mind that execution speed wasn't the point of Hack -- it was about developer efficiency. Any performance gain is probably not going to be worth the effort going from PHP on HHVM to Hack on HHVM. The gain in developer productivity, though, probably is.

那么从PHP5迁移到HHVM会加速您的代码吗?非常可能.快速转换为Hack会加快速度吗?否.添加更多类型注释会加快速度吗?也许将来一点,也许更多,但这不是重点.

So does moving from PHP5 to HHVM speed up your code? Very likely. Does doing a quick conversion to Hack speed it up? No. Does adding more type annotations speed it up? Maybe a little, maybe more in the future, but that's not really the point.

这篇关于在HHVM上,用Hack编写的代码比用PHP编写的代码快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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