What is Compiled Code

49.1 Introduction

Java is generally seen as a language that has to struggle with performance problems. Not only the processing speed of the compiler and other development tools, but above all that of your own programs, often leaves a lot to be desired. Due to the platform independence of the bytecode generated by the compiler, it cannot normally be executed directly on the respective operating system. Instead, it uses its own interpreter, the Virtual machine (short: VM), for executing the created programs.

Interpreted code naturally runs more slowly than compiled code, even if it is in the form of bytecodes. In principle, it is also possible to translate Java programs into native code (there are even some commercial tools that do this), but then the platform independence is over and the finished program only runs on one operating system. While that for Applications may be acceptable in certain cases, this approach is out of the question for Appletswhich have to run on many different browsers and operating systems on the Internet by themselves. In addition, native-compiled programs counteract the basic idea of ​​cross-platform binary compatibility, which is one of the outstanding properties of Java.

Offer an alternative solution Just-In-Time Compiler (short: JIT), the development of which is progressing in great strides. A JIT is a program that translates the bytecode of methods during the execution of the program into machine code of the current platform and can thus be executed much faster the next time it is called. The advantage here is that the class files with the bytecode can be delivered unchanged and the program is given its cross-platform character. Only the just-in-time compiler is platform-specific and tied to a certain operating system. Almost all manufacturers of commercial Java products have now integrated a JIT as an integral part of their Java development system. Since JDK 1.1.6, SUN has also been delivering the just-in-time compiler from Symantec as an integral part of the JDK.

Unfortunately, even a just-in-time compiler is not a panacea for performance problems. It is indeed able to accelerate certain parts of the code so much that their execution speed comes close to that of compiled C code. On the other hand, there are still plenty of opportunities to write programs that inherently slow Contain code that cannot be significantly improved even by a just-in-time compiler. In addition, the use of the JIT results in a certain overhead, which may prevent a net gain in performance, because the compilation of the bytecode costs time and additional memory.

Furthermore, it should be noted that a large number of checks must be carried out at runtime, which reduce the execution speed of Java programs:

  • Array and string accesses are checked for overrange.
  • Pointers are checked against prior to dereferencing.
  • Assignments of object instances are checked for correct typing.
  • There are checks on many arithmetic operations (overflows, dividing by zero, etc.).

It is therefore best to pay attention to the execution speed of the generated code during the development of the programs. In this chapter we want to look at some typical Java constructs which can lead to performance problems if used carelessly. At the same time, we want to show ways in which alternative code can circumvent the bottleneck and improve the speed of the program. If you follow these rules, it is quite possible to write larger programs in Java whose runtime behavior is absolutely acceptable on current computers.

We do not intend to delve into basic code optimization techniques in this chapter. Even if they are briefly hinted at at times, these topics can be better looked up in books about programming languages, algorithms or optimization techniques for compilers. Tips & tricks, which usually only bring marginal improvements, or slow code for which no easy-to-use alternatives are known, should not be dealt with here. Instead, we want to concentrate on a few large topics that are easy to implement and quickly lead to improvements in practice.