DNA Cafe | Download | FreeSoftware | Java | Links

DNA - Cafe - Which faster allocation performance, Java or C++?

One day, I got e-mail from my friend O. (translation in English)

Hi, can you believe ↓?

http://www.ibm.com/developerworks/java/library/j-jtp09275.html

I tried to compare new Byte[1024] to new char(1024) in my environment, C++ is several times faster...

# I don't know the case of fragment heap because I don't try it.

The real performance with real application. So, there is no point to do that.

But, can you make two same applications, one is made by Java, other is made by C++?

So, let's get organized to do the benchmark.


Benchmark programs and softwares

The programs, written by C, C++ and Java, create N objects which allocate size len memory.

source file archive → malloc_test.tar.bz2 (2643 bytes)

Languages
C GCC 3.4.4 gcc -g -O0
C++ GCC 3.4.4 g++ -g -O0
GCJ GCC 3.4.4 gcj -g -O0
Java Sun JDK 1.5.0_05 (default)

Hardware and software

The PC is ThinkPad i1620 2661-23J (M-Celeron 500MHz LV, 320MB), and OS is Linux 2.6.14 + GNU libc 2.3.6 (using NPTL). I measured user time and sys time by GNU time 1.7.

Results

Fig. 1-6 shows results, x-axis is allocated memories [bytes] and y-axis is execution times [seconds].

1-256k * 100000 times, user time
Fig. 1: 1-256k * 100000 times, user time

1-256k * 100000 times, sys time
Fig. 2: 1-256k * 100000 times, sys time

8k-1M * 10000 times, user time
Fig. 3: 8k-1M * 10000 times, user time

8k-1M * 10000 times, sys time
Fig. 4: 8k-1M * 10000 times, sys time

256k-64M * 1000 times, user time
Fig. 5: 256k-64M * 1000 times, user time

256k-64M * 1000, sys time
Fig. 6: 256k-64M * 1000 times, sys time

Discussion

Benchmark summary

Based on the result depends on the hardware and OS,

  1. In small size allocation, C++ have an advantage
  2. In large size allocation, C++ is as good as Java
  3. C language have great advantage in large size allocation (gimmcks or tricks?)

In real application

Most applications have many small size objects. Think simple, C++ seem to have an advantage. However, in such condition, memory optimization is extremely helpful.

Benchmark says GC of Java and GCJ work good, it close enough C++ in real time at large allocated memory. It suggest Java, that memory optimization work good, is equivalent to the quality of C++ in real time if the application have many small object.

There are memory management based on hardware, OS, or library, it dominate memory management of the application. But the performance depend on the Java application make effictient use of GC.

Sidebar

There are performance comparison pages in Kazuyuki Shudo's page, he is also known author of shuJIT. I think there is not so much of a difference between C++ and Java.


Reference

http://slashdot.jp/~oku/journal/327177
"Urban performance legends, revisited", revisited (Japanese)
http://www.ibm.com/developerworks/java/library/j-jtp09275.html
IBM dW:Java theory and practice: Urban performance legends, revisited
http://www.shudo.net/jit/perf/
Performance Comparison of Java/.NET Runtimes

Vector Valid XHTML 1.1! Valid CSS!