The Java Faster than C++ Benchmark
The Java is Faster than C++ and C++ Sucks Unbiased Benchmark
Introduction
I was sick of hearing people say Java was slow, when I know it's pretty fast, so I took the benchmark code for C++ and Java from the now outdated Great Computer Language Shootout and ran the tests myself.
I used G++ (GCC) 3.3.1 20030930 (with glibc 2.3.2-98) for the C++, with the
-O2flag (for bothi386andi686). I compiled the Java code normally with the Sun Java 1.4.2_01 compiler, and ran it with the Sun 1.4.2_01 JVM. I ran the tests on Red Hat Linux 9 / Fedora Test1 with the 2.4.20-20.9 kernel on a T30 laptop. The laptop has a Pentium 4 mobile chip, 512MB of memory, a sort of slow disk.The results I got were that Java is significantly faster than optimized C++ in many cases.
The results I got show that no one should ever run the client JVM when given the choice. (Everyone has the choice. To run the server VM, see instructions in the Using the Server JVM section below.)
I will post here anyone else's results as long as they use Java 1.4.2 or higher and any version of GCC that produces faster or equivalent code than the 3.3.1 I used. I encourage you to download the source and/or the binaries and perform the tests yourself, with your favorite compiler and on your favorite platform. See the Notes section below for details.
Data and Results
- Data table and graph
- Console run log
- Source:
- Binaries
- Download all source code and binaries: .bz2, (69KB) .zip (189KB)
Notes
I've been told that these tests are invalid because they were run with GCC. I have seen both benchmarks that show GCC producing faster code than Visual Studio's VC++ compiler, and benchmarks showing the opposite. If I update the benchmarks with another compiler added, it will be the Intel C++ Compiler, which I'm pretty sure produces faster code than VC++.
I've been accused of biasing the results by using the
-O2option for GCC, supposedly because -O2 optimizes for space, thus slowing down the benchmark. This is not what -O2 does. According to the GCC -O documentation:-O2: Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2. As compared to -O, this option increases both compilation time and the performance of the generated code.On the other hand, -O3 performs space-speed tradeoffs, and -O performs fewer optimizations. Thus, for these tests, I think -O2 was the best choice.I don't have an automated means of building and benchmarking these things (and the scripts that came with the original shootout didn't run for me). I really do want you to test it on your own machine, but it's going to take some work, I guess. I compiled the C++ code with:
g++ [test].cpp -O2 -march=i386 -o [test]-386
g++ [test].cpp -O2 -march=i686 -o [test]-686I compiled the Java code with:
javac [test].javaTo see how I ran the binaries, see the run log. You can download the source code I used in .bz2, .zip format.
Using the Server JVM
There are two ways to run Java applications with the server VM:
- When launching a Java application from the command line, use
java -server [arguments...]instead ofjava [arguments...]. For example, usejava -server -jar beanshell.jar.- Modify the
jvm.cfgfile in your Java installation. (It's a text file, so you can use Notepad or Emacs to edit it.) This is located in C:\Program Files\Java\j2reXXX\lib\i386\ on Windows, /usr/java/j2reXXX/lib/i386/ on Linux. You will see two lines:You should change them to:-client KNOWN -server KNOWNThis change will cause the server VM to be run for all applications, unless they are run with the-server KNOWN -client KNOWN-clientargument.Contact
I can be contacted at keith@kano.net.
Links / Responses
A poem from Rokky05:
- Java vs. C benchmark - the benchmark is in Microsoft Word format
- FreeTTS performance case study - at Sun Research
- Linux Number Crunching - used 1.4.1 VM, got very different results than I did, maybe due to floating point optimizations added in 1.4.2
- SNAP matrix benchmark - Java vs. C++ for matrix multiplication
- Nine-Language Performance Round-Up - math and file I/O performance among nine languages
- Performance comparison C++, C# and Java - similar results as mine, mostly
- Java vs. C - for bubble sort, IBM's 1.3.0 client JVM comes out on top over GCC with full optimizations
- Runtime Vs Static Compilation - Performance Comparison - even old versions of Java can run fast
- Comparing C++ performance with Java - these are my benchmarks, except more hand-optimized and compiler-optimized (Java wins about half of them)
- Java Pulling Ahead? - "finds that Java performance on numerical code is comparable to that of C++, with hints that Java's relative performance is continuing to improve"
- Benchmark C++ vs JAVA - ran my benchmarks and added GCJ and Java 5.0 results
- C++ vs Java - David K. McCombs doesn't like my measurement of CPU time and some of the benchmark's C++ code
- 'The Java Faster than C++' Benchmark Revisited - someone doesn't like my benchmarks
- Performance Comparison of Java/.NET Runtimes - lots of VM's compared
- The Java (not really) Faster than C++ Benchmark - another benchmark with hand-optimized C++ code and different gcc flags
- Java Performance Urban Legends by Brian Goetz, discusses garbage collection vs. manual allocation & deallocation
- New - March 2008 - Yet Another Java vs. C++ Shootout - hand-optimized - 64-bit - GCC 4.2.2 vs Java 6
Full nonsense did not see not when rapid program on Java, as server so client. Yes you what, boys, Java-machine together with the famous fitter garbage is written on what ... at least on C, possibly even with the use of classes, and this already C++ (on what to be yet written her not on asme certainly), But as C can work quick than itself???? About the author of this article: All that a fool does, all he does not so !!!!! Not article, and some delirium! Request to the author of this article: Write what firm do you work on and who? Such feeling, that you see a computer only then when you write the articles!This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
