Monday, March 5, 2007

Java Performance Myth

There is another discussion on our local java user group mailing list regarding java performance issue compare to application built using C/C++. One of the poster also comment on how an empty for-loop that run for 1,000,000,000 (yes, that is one-billion) iterations on java is three times slower than a c++ one.

In the beginning I started replying with the standard answer of how java requires jvm startup time which shouldn't be counted and how empty for-loop is not a good measurement, etc. By the time I finished typing the response, there are plenty of other posters responding already. So I did not even bother to send my email.

But it makes me wonder about the jvm startup time, since I heard that it has improved a lot with JSE 5 and 6 (aka 1.5 and 1.6). So I set out to do the experiment with the given parameters of doing a billion iteration of empty loop.

I decided to give the java, C, and C++ a try. My testing environment is Ubuntu 6.10 running in vmware (see my previous post). Details of the environment I used for testing are:
  • JSE JDK 5 from Sun
  • JSE JDK 6 from Sun
  • gcc & g++ 4.1 that came with the Ubuntu

I tested the following source code.

Java:


public class Loop1 {
public static void main(String[] args) {
for (int i = 0; i < 1000000000; i++) ;

}
}

public class Loop2 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) ;
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - start)/1000.0 + "s");
}
}


C:


int main() {
int i;
for (i = 0; i < 1000000000; i++) ;

return 0;
}


C++:


int main() {
for (int i = 0; i < 1000000000; i++) ;

return 0;
}

I used the following commands to compiled those codes:
  • javac Loop1.java
  • javac Loop2.java
  • gcc loop.c -o loopc
  • gcc -O3 loop.c -o loopcO3
  • g++ loop.cpp -o loopcpp
  • g++ -O3 loop.cpp -o loopcppO3

and I run them with time command 3 times and take the average of the real time output from the time command, in the case of Loop2 I took the average from the output result.

And the result are:

Loop1 (Java5) - 1.793s
Loop2 (Java5) - 1.538s
Loop1 (Java6) - 1.468s
Loop2 (Java6) - 1.233s
loopc - 2.357s
loopcO3 - 0.008s
loopcpp - 2.384s
loopcppO3 - 0.011s

Draw your own conclusion ;P

P.S.: I also tried an empty java program, to see the startup time and to compare it with the removed for-loop for gcc/g++ compiler with -O3 flag. The average result was 0.139 seconds.

Trying Ubuntu 6.10 on VMWare

I haven't been using linux for a while; last time I used it for daily stuff is probably 4-5 years ago at work. My first experience with linux was with Redhat 5.X which I bought from Tattered Cover; and the last time I used linux extensively was with Mandrake 8. Basically my previous experience were on RedHat-derived system using rpm for the package management, which is quite different from Ubuntu. Since then I mostly setup linux for development servers, for running tomcat/jboss, cvs/subversion, etc. So my linux knowledge is really obsolete and not very well verse on the desktop end.

I recently gave Ubuntu a try because I want to explore more on using linux as my daily working environment. I know..I know a lot of people has been using it daily,..I just been away too long, okay? The reason I chose Ubuntu because I just need something simple and I don't want to spend to much time tweaking and configuring the system. I just need something that will let me do what I want to do, which is mostly coding and browsing. I did not do a fresh installation of Ubuntu yet, instead I am giving it a try running on VMWare. I grabbed Ubuntu version 6.10 vmware from here.

I am quite happy with the default vmware image I had downloaded; it have most of the stuff I need. I do not need any specific stuff, since I am using it mostly for development ruby stuff. The only packages that I installed outside the default are: ruby, irb, rdoc, mysql, and full vim support.

Running Ubuntu under VMWare is pretty smooth, the only extra configuration that I have to do are the XOrg configuration for the video card and mouse driver. The video card configuration to allow higher resolution and the mouse driver change is for fixing the slow and jerky mouse response when running under vmware. I wanted to post the links to the information on how to do the video and mouse configuration, but I had lost the links and too lazy to find them again. I can tell you that I found the information from vmware's forum and ubuntu's forum.