HHVM vs PHP is a battle that you should consider. Which one is more faster. Is HHVM more performant than PHP? Does HHVM consume less CPU than PHP? Which one is more stable and which one could be used in production? These are questions that might get you interested in learning about the differences between HHVM and PHP.

One example of how to use HHVM (Hip Hop Virtual Machine) is Wikipedia. Wikipedia is one of the top 10 biggest websites in the world. In case you don’t know, recently, Wikipedia dropped PHP5 and is now running on HHVM.

It took six months for Wikipedia to deploy a new technology for MediaWiki, Wikipedia’s underlying PHP-based code. The conclusions were clear, HHVM runs much faster than PHP5. More exactly, Hip Hop Virtual Machine, or HHVM, reduces the median page-saving time for editors from 7.5 seconds to 2.5 seconds, which means that there’s a mean page-saving time from about 6 to 3 seconds.

How HHVM Works?

Before knowing how HHVM works, you must know how PHP works. PHP is a dynamic, interpreted language. So, like all interpreter languages, PHP has the inherent performance disadvantage. HHVM gains a lot more speed by using just-in-time (JIT) compiler. In this way it optimize the compiled code while the program is already running. It’s true that PHP is called a dynamic language, but the types used in PHP programs are not exactly dynamic. Knowing this issue, HHVM observes the types present at runtime and generates machine code optimized to operate on these types.

Let’s take this example: You have a website and you access a route (a page) that will load a certain file. This is how HHVM will treat this case.

HHVM checks a code cache stored in a SQLite database to see if it has already compiled the file that is requested. Similar to how APC caches PHP’s compilation process, HHVM will execute the cached version of a file, if there is one. If not, HHVM’s analysis engine is invoked to compile the script, perform type inference, optimization and store the result for the next time when the file will be requested.

Just in Time

One of the main features of HHVM and one that you will certainly want to use in production is JIT compiler, or, just in time compiler. If you enable JIT compiler, the server will turn the bytecode from hhvm.hhbc.sq3 into native code. This will result in performance boost. The performance improvement is up to 300% in some cases.

You can enable the just-in-time compiler from the file config.hdf :

Eval {
Jit = true

You can see below some graphics regarding Wikipedia’s transition from PHP to HHVM

In the above image you can see that the median page time saved on the HHVM servers was about 45% of PHP’s. This translates to faster page loads, so that’s why everytime someone previews or edit files, they get the result back twice fast.

But this improvement raises a question. We’re used to know that faster pages means CPU loads, lots of disk-writes and lots of database calls. The interesting thing is that while running on HHVM the CPU load is reduced drastically. In the below image you’ll see the CPU load before and after using HHVM:



At this moment PHP is a mature language. The latest version is PHP 5.6. There are lots of differences between PHP 5.3 and PHP 5.6. PHP 5.6 is much better. But there’s a problem. A big percent of the hosting provider companies still uses PHP 5.3 and in this way the clients of these companies struggles to keep up with the websites that run under PHP’s newest versions, such as PHP 5.5 and PHP 5.6. But there’s a big question that needs answered. In 2014 Facebook launched HHVM. Hip-Hop Virtual Machine applications run much faster than the basic PHP applications. Facebook open-sourced this PHP interpreter to the large audience. HHVM is based on PHP, but Facebook decided to get rid of lots of unnecessary functions. This is a good thing, but converting your application from PHP to HHVM can be hard to do. The PHP team decided that this year will release PHP7, the answer to HHVM. So we raise the following questions:

The battle between PHP and HHVM is leaded by HHVM at this moment.
Rumors say that PHP will launch this year the version 7. Will PHP 7 be faster than HHVM?
Does PHP uses JIT compiler or not?
Who will be faster? Who will be more secure?
Which one will have the smallest CPU load and which one will consume fewer RAM memory?

The answer is close. Stay tuned and read the latest tech and programming news on The Hacking News blog. You will be the first one to know the answer to all these questions.