How can Java be faster?

Posted by Andy Gavin on Thu, 2005-11-24 17:58

I've just remembered two, perhaps rhetorical, questions that I was once asked:

  1. How can ever Java be faster than native code when it runs in a virtual machine?
  2. When I miss a semicolon of the end of a compilation line, why doesn't it just insert the semi-colon and continue? If it knows what's wrong with it, it should just fix it?

Both these questions are based on a misunderstanding of what a compiler actually does. First, how can Java be faster? the answer lies in how a native compiler produces native-code. It is well known that the way that a programmer lays out code more often than not laid out to be best executed. The structure, especially in large systems is laid out to be understood more readily. It has been left to a vast degree up to the compiler and hardware to gain the performance increases. Compiler optimization, at best is static, either the programmer:

  • goes to great lengths working out what the optimum output for bottleneck algorithms should be and optimising these (sometimes this involves breaking the structure)
  • or, does some test runs through the system collecting profiling information and feeds this into the compiler.

How can Java be faster than native
Optimization is hard, much advice exists that says to ignore this sort of optimization altogether. However, the real benefit of a virtual machine, once it is up and running, is that it can descide which portions of the system are bottlenecks and optimize these in a Just-In-Time manner. So in the dynamics of programs running, Java could end up being the faster more maintainable solution. What hasn't changed for, whatever language is used is the how you solve the problem can make all the difference. The important thing it what you write; get the algorithm right.

(NB. I'm not certain but the idea of JIT compilation probably comes from Just-In-Time Manufacturing)

The other thing that has begun to happen in the industry is better processor speed increases have recently been achieved by adding additional "cores" to chips. This has meant an increasing prevalence of parallelization of algorithms, The Java VM is well placed to take advantage of this: the VM memory model is optimized to manage concurrent threads. Getting multicore programming right for an application by directly targetting the native architecture is much more difficult.

Why doesn't the compiler just insert the semi-colon, if it knows it's missing?

This is to do with language grammar. In languages that use the semi-colon at the end of a statement they do so to allow greater expression with statements. The semi-colon delimits the statement, without it particular types of statements like expressions run into one another. Not all statements, however are ambiguous in these grammars, in these cases the compiler is able to expect a semi-colon. When the compiler detects these cases, it does all but insert the semi-colon as part of the error-recovery; allowing the compiler to continue to find more errors. If the semi-colon could be missed it would create inconsistency between the ambiguous and non-ambiguous statements; it would also complicate the grammar.



I found this additional link that shows the performance of the JVM. What is interesting is that the server side JVM is the best performing in most cases. The client-side JVM doesn't perform as well.

Add new comment