PHP is the most popular server side scripting language of the web since it’s free, easy to learn and a reasonably thought out. According to Wikipedia it is installed on more than 20 million websites.
Yet how do you make it run faster? It will be in your best interest to do so when 10,000 people are playing your game at the same time.
You can measure the time it takes to build a page output by PHP by putting this line at the top of the script:
<php $pst = microtime(true); ?>
and this one at the bottom:
Page Built in <!--?php echo number_format(microtime(true)-$pst,6);?> Secs
The <!–?php and ?> are special tags that separate normal html from the PHP script.
On a shared Apache server, a page from my website without any database access takes around 0.000633 seconds to build. (Scroll to the bottom and you’ll see I’ve added this calculation and message to every page on the site). This time is dwarfed by the time it takes the web server to compress the page, send it to the browser which then decompresses and displays it. Using Firefox with an extension to show page load times reveals that the total time to fetch and display the page (including advert) is 1.5 seconds, though both times will vary by time of day.
One way to make PHP faster is utilizing the couple of caches available, yet this requires access to the underlying Operating system, which service providers of shared web servers are understandably reluctant to do. You need a dedicated server for that. Another way is install the Zend server, which comes at a hefty price tag.
The third way is to compile PHP – one for Linux and one for Windows. Scripting languages are not fully compiled and tend to run slower than fully compiled programs.
For Linux, Facebook produced a PHP to C++ converter called hiphop that they open-sourced and released two years ago. According to Facebook’s engineering, WordPress (which is written in PHP) is 2.7x faster with hiphop. They’ve even worked on versions for other platforms such as Mac OS X, which they don’t use at Facebook. That’s a nice contribution to open source.
For Windows, there’s an alternative compiler for PHP called Phalanger. This not only compiles PHP to .NET assemblies, but lets you mix .NET code with it. It’s written in C# and should in theory give code the same speed or pretty near to ASP.NET.
Both HipHop and Phalanger offer another advantage which shouldn’t be ignored. PHP is a scripting language so anyone who gains access to your server can copy the code, look for passwords, vulnerabilities, etc. If it’s compiled with C++ (HipHop) or C# (Phalanger) then gaining access will never reveal your source code or secrets.
If you’re not bothered about performance but just want to protect your PHP source code (maybe you are licensing it) then 3rd party vendors like Zend or ionCube sell software that will protect it.
In my game series, I’m developing code on a Windows virtual server and will be trying out Phalanger, so expect to hear a bit more about my experiences with it.