The interpretation approach to program execution provides
for transportable code but usually at the cost of slower execution.
Advances in Java Virtual Machine (JVM) design, however, have greatly
enhanced Java performance and Java programs can now reach speeds
approaching that of C/C++ for many applications.
The figure below illustrates the essential steps in
running a Java program. First is shown the bytecode for a multiplication
of two floating point numbers.
The most basic VM would simply interpret each line
of the bytecode sequentially. (Of course, it will occasionally jump
to a different section of code when it executes a jump command.)
If written in C, it might use a long switch
statement with a case for each possible bytecode instruction.
Alternatively, a JVM that includes a Just-In-Time
(JIT) compiler will instead convert the instructions to native
machine language the first time it encounters these instructions.
That is, it compiles on the fly or just-in-time to execute
Note that here compile
refers to the conversion of bytecode to local machine code, not
the conversion of Java source code to bytecode.
In the first pass this compilation will slow the program
somewhat. However, for code sections that run repetitively, such
as a loop in a math
computation, the native code executes on the subsequent passes and
thus provides the full performance capabilities of the platform.
Sun's Hotspot JVM takes an even more sophisticated
approach. It dynamically monitors, or profiles, a program's performance
as it is running and looks for sections of the code where execution
slows significantly. When it finds such a section, it will optimize
that code and compile it to native machine language for faster execution.
In such cases, the optimization may result in speeds that actually
exceed those for C programs.
A crude but effective approach, at least as far as
speed is concerned, is to compile the bytecode directly to local
machine code. So-called native code compilers and ahead-of
-time compilers will provide very fast performance but, of course,
at the price of eliminating the platform independence of the code
and preventing dynamic binding.
References & Web Resources
Latest update: Oct. 5.05