Java is Slow Revisited

By Angsuman Chakraborty, Gaea News Network
Sunday, August 26, 2007

There is a minor-war going on in Sun Alumni mailing list and elsewhere where the common misconception: “Java is slow” is surfacing again and again, much to the dismay of knowledgeable Java developers who have known for ages that Java is fast even compared to C++ and has been so for several years. All this re-surfaced because Sun decided to change its ticker symbol to JAVA (previously it was SUNW). All of this is terribly frustrating to Java developers who take pride in Java and are well aware of its capabilities because they leverage it every day. So let’s rest this myth to rest once and for all, shall we?

Myth 1: Java is Interpreted language and so it must be slow

Java is currently just-in-time-compiled by the JVM (Java virtual machine). Just-in-time-compilation gives it equivalent performance to compiled applications, sometimes better. Sun’s JIT is not the only one either. IBM and others provide their implementation of JVM with just-in-time-compilation too.

You can also compile Java programs in native executables with free compilers like gcj ( https://gcc.gnu.org/java/ ) or paid ones like Excelsior JET ( https://www.xlsoft.com/en/products/jet/index.html ).

Java’s slowness is not intrinsic, that is an architectural mis-conception as demonstrated above. But wait there’s more..

Java versus C++ Benchmarks

Let’s look at some benchmarks comparing Java with C++.

Performance of Java versus C++ by J.P.Lewis and Ulrich Neumann from Computer Graphics and Immersive Technology Lab, University of Southern California

https://www.idiom.com/~zilla/Computer/javaCbenchmark.html

This article from 2003 finds “This article surveys a number of benchmarks and finds that Java performance on numerical code is comparable to that of C++, with hints that Java’s relative performance is continuing to improve. We then describe clear theoretical reasons why these benchmark results should be expected.”

I would like to quote even further as they gave a detailed explanation about the slowness myth of Java and dispels it thoroughly:

Conclusions: Why is “Java is Slow” so Popular?

Java is now nearly equal to (or faster than) C++ on low-level and numeric benchmarks. This should not be surprising: Java is a compiled language (albeit JIT compiled).

Nevertheless, the idea that “java is slow” is widely believed. Why this is so is perhaps the most interesting aspect of this article.

Let’s look at several possible reasons:

* Java circa 1995 was slow. The first incarnations of java did not java a JIT compiler, and hence were bytecode interpreted (like Python for example). JIT compilers appeared in JVMs from Microsoft, Symantec, and in Sun’s java1.2.

This explanation is implausible. Most “computer folk” are able to rattle off the exact speed in GHz of the latest processors, and they track this information as it changes each month (and have done so for years). Yet this explanation asks us to believe that they are not able to remember that a single and rather important language speed change occurred in 1996.

* Java can be slow still. For example, programs written with the thread-safe Vector class are necessarily slower (on a single processor at least) than those written with the equivalent thread-unsafe ArrayList class.

This explanation is equally unsatisfying, because C++ and other languages have similar “abstraction penalties”. For example, The Kernighan and Pike book The Practice of Programming has a table with the following entries, describing the performance of several implementations of a text processing program:
Version 400 MHz PII
C 0.30 sec
C++/STL/deque 11.2 sec
C++/STL/list 1.5 sec

Another evidently well known problem in C++ is the overhead of returning an object from a function (several unnecessary object create/copy/destruct cycles are involved).

* Java program startup is slow. As a java program starts, it unzips the java libraries and compiles parts of itself, so an interactive program can be sluggish for the first couple seconds of use.

This approaches being a reasonable explanation for the speed myth. But while it might explain user’s impressions, it does not explain why many programmers (who can easily understand the idea of an interpreted program being compiled) share the belief.

Two of the most interesting observations regarding this issue are that:

1. there is a similar “garbage collection is slow” myth that persists despite decades of evidence to the contrary, and
2. that in web flame wars, people are happy to discuss their speed impressions for many pages without ever referring to actual data.

Together these suggest that it is possible that no amount of data will alter peoples’ beliefs, and that in actuality these “speed beliefs” probably have little to do with java, garbage collection, or the otherwise stated subject. Our answer probably lies somewhere in sociology or psychology. Programmers, despite their professed appreciation of logical thought, are not immune to a kind of mythology, though these particular “myths” are arbitrary and relatively harmless.

Here is another benchmark on Java versus C++

The Java is Faster than C++ Benchmark

https://www.kano.net/javabench/
This was also quoted in JDJ magazine article: https://java.sys-con.com/read/45250.htm

“I used G++ (GCC) 3.3.1 20030930 (with glibc 2.3.2-98) for the C++, with the -O2 flag (for both i386 and i686). I compiled the Java code normally with the Sun Java 1.4.2_01 compiler, and ran it with the Sun 1.4.2_01 JVM. I ran the tests on Red Hat Linux 9 / Fedora Test1 with the 2.4.20-20.9 kernel on a T30 laptop. The laptop has a Pentium 4 mobile chip, 512MB of memory, a sort of slow disk.

The results I got were that Java is significantly faster than optimized C++ in many cases.
JVM startup time was included in these results. That means even with JVM startup time, Java is still faster than C++ in many of these tests.

BTW: The article also links to lots of other benchmarks at the end.

Hopefully I have finally laid rest to the myth of “java is slow“. It is a perception, a blatantly false perception, not a reality. Get over it.

Discussion

Barry
March 4, 2010: 4:31 pm

The main issue I see is with applets. Loading the JVM takes on some systems > 5-10 seconds. The applet i have created loads in < 1 second so it is really an issue with the browser loading the JVM then loading the applet code itself. Not much you can do with code if the platform itself takes an hour to boot.

December 22, 2009: 3:37 am

Java ist often just made slow by programmers. Often it’s just small little things that need to be changed to make s.th blazingly fast.

But without profiling and knowing the underlying api you’re doomed - as in every other langauge as well!

See my sorting-post for example: https://frickelblog.wordpress.com/2009/12/21/avoid-too-much-sorting/

2 lines of ‘convenient code’ that madea difference between 3sec and 90ms ..

September 25, 2007: 10:06 pm

Thanks for the link.

August 26, 2007: 9:39 pm

I wrote a similar piece in my blog a few months ago which ended-up being in the Java Developers Journal as well, but I thought you’d find it somewhat relevant and interesting: Correcting Logical Fallacies: Why Java Is Not Slow

August 26, 2007: 10:02 am

Weblogic is slow, has been slow for ages. There is a good reason why they don’t allow any performance tests of their application server. However that doesn’t affect Java’s performance. Try running a simple jsp on tomcat. It is lightning fast.

August 26, 2007: 5:52 am

Maybe you haven’t had to deploy an EAR to Weblogic and call just a simple JSP. You would understand easily why some developers think Java is slow…

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :