Java Speed Tests


Recently I have performed some informal speed tests comparing C++ and three different Java systems on the PC. The tests consisted of generation, inversion (via Gauss-Jordan elimination with partial pivoting), and multiplication of N x N double precision matrices for various values of N.

All tests were perfomed on a 120 MHz Pentium system running Windows95. All times listed are approximate (given by system fns in each language), although they are repeatable and are very close to actual "wall clock" times. All development systems were used "as is" out of the box (e.g. no optimization, debug versions). The same source code was used in each Java system. The Java code was "ported" from C++.

                                 Matrix inversion

Compiler/runtime        N = 50    N = 100    N = 150    N = 200

 MS VC++ 4.0            0.88s     2.97s      8.57s      20.49s
 MS J++/MS Jview        0.33s     2.31s      6.7s       15.65s
 Cafe/Cafe Java         0.55s     3.24s      10s        23.07s
 Sun Javac/Sun Java     4.51s     32.03s     n/a        n/a
 
                               Matrix multiplication

Compiler/runtime        N = 50    N = 100    N = 150    N = 200

 MS VC++ 4.0            0.16s     1.2s       4.34s      12.36s
 MS J++/MS Jview        0.16s     0.88s      3.18s      9.61s
 Cafe/Cafe Java         0.22s     1.37s      4.99s      14.28s
 Sun Javac/Sun Java     1.86s     15.1s      n/a        n/a
Note that both MS Jview and Cafe Java are JIT compilers, while Sun's Java is an interpreter (hence the 10x speed difference). Although the tests show the MS J++/MS Jview combination to actually be faster than C++ (!), my conservative conclusion is that for practical purposes Java is now as fast as C++ for numerical calculations.

See the web page http://www.intergalact.com/java/ex7.html for a simple example applet which calculates and displays a 3d wireframe surface which can be manipulated by sliders and the mouse. This example runs very fast under MS IE 3.0.



Source Code

You can download and try the source code for both the C++ and Java applications. Note that you will need to remove the ".txt" suffix from the files before compiling. Also remember that Java is case-sensitive, so the capital letter on the filenames is necessary.

C++ Source Code

Java Source Code



Caveats

Since I posted these results, several people have written me with comments and test results of their own. Thanks very much to everyone who wrote! Mostly, these have been people who maintain that C++ is still much faster than Java. Their comments tend to fall into two categories:
  1. By using a fully-optimized, release-version of a C++ compiler, one can obtain speedups of 2-3x over those I list. This is probably very true. However, my tests compare one non-optimized debug-version compiler against another. As soon as there is a fully-optimizing release-version Java compiler, I would expect a similar speedup.

  2. My code is very inefficient, and that by restructuring the array type and math operations, and by using pointers in C++, one can get a speedup of 2-3x in either language. This is probably very true too. However, my goal was not to produce the fastest array program possible, my goal was to show that similar programs run under C++ and Java run about as fast. I think I've shown that. For people like myself, who are considering porting currently platform-dependent C++ software to Java, this is a very significant result.

The bottom line appears to be this: by hand-optimizing your code, and by using optimizing compilers, you can get improved performance.



Go to: Sky Coyote's home page
Go to: Some examples of Java work
Mail to: sky@intergalact.com
© Copyright Sky Coyote, 1996-7.
Last updated: 4/26/97