Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Performance tests show Java as fast as C++

Java has endured criticism for its laggard performance (relative to C++) since its birth, but the performance gap is closing

  • Print
  • Feedback

Page 3 of 7

The average user will use only a small fraction of the features at any given time. If the program was written in C++, the user would have to load the entire file before proceeding. If the program is written in Java, only those features immediately needed are downloaded, such as the main window. The user downloads additional features only as she needs them.

Real-world example (LPE)

The table below shows the sizes of the programs and resources (if applicable) used in this article. The huge size differences in the C++ versus Java programs are due to the size of the libraries that are required for the C++ program to run. The difference in the resource sizes is due to the compression difference between the GIF files used in the Java program versus the bitmap files used in the C++ program.

Program Name Program Size: C++ vs Java Resource Size: C++ vs Java


Simple Loop 46K vs 3.9K N/A


Memory Allocation 34K vs 1.4K N/A


Bouncing Globes 103K vs21K 485K vs153K


Running Program Instructions (RPI)

Once the program executable is loaded, its instructions must be executed by the target platform's CPU. In traditional C++ programming, these executable files contain binary instructions that are executed by the target platform's CPU. A developer must create a different executable by recompiling the original source code for each target platform. In addition, the peculiarities of each target platform usually forces the developer to modify the original source code.

In contrast, the executable files (called class files) produced by a Java compiler contain collections of platform-independent bytecode which cannot be run on a target platform without translation into binary instructions suitable for each target platform's CPU. The JVM is responsible for performing this translation. There are two possible methods a JVM uses to do so: a bytecode interpreter or a just-in-time (JIT) compiler.

Performance implications: Java versus native C++ (RPI)

Therein lies Java's bad reputation. Most performance perceptions for Java were derived from older JVMs that included bytecode interpretation as the only method for running program instructions.

Bytecode interpreters perform many times slower than comparable C++ programs because each bytecode instruction must be interpreted every time it is executed, which can lead to a great deal of unnecessary overhead. For example, if you code a repeat loop, and that loop executes the same set of bytecode instructions many times, the JVM will have to perform the exact same interpretation process on every instruction over and over again, each time it processes an iteration of the loop.

The perceptions earned by early JVMs are no longer valid, since most JVMs are delivered with JIT compilers. A JIT compiler translates and stores the entire class file. This eliminates the need for repeated translations of each bytecode instruction.

Performance among compilers

C++ compilers are able to improve the performance of a piece of code by detecting and improving inefficiencies through a process called code optimization. For example, a good compiler can detect if the programmer was sloppy and performed a "static" calculation within a loop. In this case, even though the calculation takes place within the loop, its results remain constant throughout the loop no matter how the program is used.

  • Print
  • Feedback

Resources
  • "CompilersPrinciples, Techniques, and Tools" by Alfred Aho, Ravi Sethi, and Jeffrey Ullman, 1986, Addison-Wesley Publishing Co. http://www.clbooks.com/sqlnut/SP/search/gtsumt?source=javaworld&isbn=0201100886
  • "Crafting a Compiler," Fischer, Charles N., LeBlanc, Richard J. Jr., Benjamin Cummings Press, 1988 http://www.clbooks.com/sqlnut/SP/search/gtsumt?source=javaworld&isbn=0805332014
  • Give us your opinion on the difference in performance between Java and C++. Take our latest reader poll. http://nigeria.wpi.com/cgi-bin/gwpoll/gwpoll/ballot.html