Performance: Java vs. PHP vs. Scala

The topic of performance of specific programming languages has provided material for endless debates among developers. Proponents of a certain language often cite examples that show their chosen language in a particularly good light. For example, I have come across the claim that PHP and Ruby are performing just as well as Java. Often these claims are supported by nothing but a firm tone of voice and a stern mien.

Thankfully, we have benchmarks. A playful yet credible approach to performance measurement was taken by this website. It contains an array of typical benchmark algorithms implemented in 35 popular (and not so popular) programming languages all running on various Linux servers. There are many complexities involved in measuring programming languages execution speed. The web development arena adds even more complexities. Much depends not only on how the algorithms are implemented, but also on how the compiler and the server are configured. For example, there are different JVMs, there are script caches, accelerators, server tunings, and what not. The languages I’d like to compare here are Java, Scala, and PHP. You will find comparison graphs for each of these below.

Three charts: 1. Java vs. PHP 2. Scala vs. PHP 3. Scala vs. Java Java, Scala, PHP Benchmarks

Three bar groups: 1. Execution time 2. Memory use 3. Size of source code Bar pointing down means: faster performance, lower memory usage, and reduced source size. Basically, these charts confirm what experienced programmers already know. Both, Java and Scala outperform PHP by several magnitudes. To be precise, Java and Scala execute 28 times faster on average.

The performance of Java and Scala is similar, which has to be expected since both languages run on the Java virtual machine. Java gives slightly better performance in some algorithms while its memory requirements are lower. For PHP fans who find these benchmarks disappointing, there is some consolation. PHP appears to be less of a memory hogger, and perhaps more importantly, it performs quite favorably when compared to other dynamic interpreted languages, such as Perl, and Ruby. You can compare more language benchmarks yourself at the following URL:

7 thoughts on “Performance: Java vs. PHP vs. Scala

  1. I am not entirely convinced by this argument. Optimising code is certainly the best way to gain performance, but in the “real world” software is rarely optimised and changing it may be costly or impossible. Using JVM languages vs. interpreted languages usually means that your server can handle more requests per second, since the former are less CPU-intensive. This translates into tangible benefits, such as more concurrent users, better burst load performance, etc. The one thing where PHP has an edge is better support for virtual hosting, since users don’t need to share a JVM. But that’s a different story.

    Cheers, Thomas

  2. I don’t think this comparison is correct in detail because results may strongly depend on how e.g. php is setup. There are lots of tricks and tweaks so the absolute number given in this charts might not fit real productive systems. But the overall better performance of precompliled languages over interpreted languages like php is not denialble.
    Performance is not the right question as every huge website do either cache (e.g. akamai) or avoid executing dynamic sites and therefore its server(s) store and deliver plain html (which is btw. way faster than everything else). Combinations are possible. By doing the right mix servers/DBMS can be discharged by estimated 80%.

  3. While I can’t vouch for the correctness of the data, it is important to note that what is measured here is CPU time. Therefore, caching, even object caching, is unlikely to affect the results.

    Cheers, Thomas

  4. so if you want to implement some “algorithm”, like BinaryTree and FASTA, dont use PHP.
    but in the real world, when the web applications spend 95% of the time waiting for resources like filesystem, sql, memcache, etc. you wont see this magnitude of difference…


  5. I don’t get it. The same optimizations that can work for PHP also apply to Java/Scala apps right? The response from the dynamic language folks is the server is not configured correctly.

    Java/Scala has a world class VM. There is a reason projects like HipHop for PHP and alternatives Ruby implementation exists. Dynamic languages require more hardware. In the long run that outweighs the initial productivity gained by dynamic languages.

    I love Sinatra and Rails but they simply use too much resources. I can serve many more requests from a JVM based solution and that is fact.

  6. Wouldn’t a comparision betwee Java and Quercus or Resin be better, as PHP would be using the JVM then