free ebooks

 

 

 Home
105 Computers & InternetProgramming > Java  
Java Performance

Java(tm) Runtime Speed

One of the things you may notice fairly quickly is that Java programs can run more slowly than their counterparts in other languages. For example, this Java program:

        // sort numbers            class bm1 {                  static final int N = 6500;                  public static void main(String args[])                  {                          int i;                          int j;                          short t;                            short vec[] = new short[N];                            // seed with descending order                            for (i = 0; i < N; i++)                                  vec[i] = (short)(N - i);                            // sort into ascending order                            for (i = 0; i < N - 1; i++) {                                  for (j = i + 1; j < N; j++) {                                          if (vec[i] > vec[j]) {                                                  t = vec[i];                                                  vec[i] = vec[j];                                                  vec[j] = t;                                          }                                  }                          }                  }          }  

runs about 35-50 times slower than its C++ equivalent (with both JDK on Windows NT and on the Mac with Roaster):

        // sort numbers            #include <stdio.h>            const int N = 6500;            short vec[N];            main()          {                  int i;                  int j;                  short t;                    // seed with descending order                    for (i = 0; i < N; i++)                          vec[i] = N - i;                    // sort into ascending order                    for (i = 0; i < N - 1; i++) {                          for (j = i + 1; j < N; j++) {                                  if (vec[i] > vec[j]) {                                          t = vec[i];                                          vec[i] = vec[j];                                          vec[j] = t;                                  }                          }                  }                    return 0;          }  

This is due to the interpreted nature of Java and also due to the extra runtime checking that it does (such as for out of bounds vector subscripts). This particular sorting algorithm isn't very efficient (it has N**2 performance), but is illustrative of the point.

Consider another Java program:

        // number crunching            class bm2 {                  public static void main(String args[])                  {                          long i = 1200000L;                          double d;                          while (i > 0) {                                  d = Math.sqrt((double)i) + Math.log((double)i);                                  i--;                          }                  }          }  

This program is only about 3 times slower than the equivalent C++:

        // number crunching            #include <math.h>            main()          {                  long i = 1200000L;                  double d;                    while (i > 0) {                          d = sqrt(double(i)) + log(double(i));                          i--;                  }                    return 0;          }  

Why the difference? Well, in this second program, most of the time is taken in executing the math functions sqrt() and log(). These functions are typically implemented in hardware or via low-level software routines ("native" methods). If most of the time is spent in them (math functions are typically quite expensive), then the interpretive overhead required to handle the loop iterations and so on is less important.

In many cases this difference in performance isn't very important. For example, if a Java program or applet is highly interactive or operates across the Internet, then the delays incurred by waiting on humans and networks will be more important than the raw speed of the language.

It remains to be seen just how Java performance will shake out. There are several possibilities for improving it. One is to generate native code, but this works against the portability of Java applets and applications. Another similar technique that has been mentioned is a Java to C translator ("java2c"), but this also works against portability.

There is a technique called "just in time" compilation that is being considered. The technique involves doing dynamic compilation within the bytecode interpreter, so that the next time a bytecode is executed, the actual machine code for a given platform is substituted in its place.

Download

 Additional Info
 
 No. 253
 Posted on 8 June, 2006
 
218
 
 
Bookmark This Page
Facebook Twitter stumbleupon

Share
Email
Google

Link to us from your website or blog by using the code below in your html
   
@2008 ebooklobby privacy policy email: info [at] goaheadweb.co.uk