Performance comparison C++, C# and Java

Test System
  • AMD Athlon XP 2200+
  • 1024 MB DDR 266
  • VIA KT266A
  • Windows XP Prof. + SP1

Java Version + Options
  • Java 1.4.2_03
  • Java 1.5 (alpha, 11. Dec. 03)
  • compiler options: -g:none
  • running options: -mx1024m

C# Version + Options
  • .Net Framework 1.1.4322 (Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4)
  • compiler options: /optimize+ /debug- /checked-
  • running options:

C++ Version + Options
  • Microsoft Visual Studio Net 2003 (Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86)
  • compiler options:  /O2 /Oi /Og /Ot /EHsc /arch:SSE
  • Intel(R) C++ Compiler for 32-bit applications, Version 8.0   Build 20031017Z
  • compiler options:  /fast /G6 /Oi /Og /Ot /EHsc /arch:SSE
  • running options:

Source Code
Original Source Code is from Christopher W. Cowell-Shah http://www.cowell-shah.com/research/benchmark/code and from http://dada.perl.it/shootout/ and Doug Bagley http://www.bagley.org/~doug/shootout . I took portions and whole parts from both and put them into a single file and did some changes + bugfixes.


Results
Maximum memory usage:
  • Java - 163 MB
  • C# - 111 MB
  • Cpp - 98 MB
Performance in ms:

Cpp is the fastest, except the STL "hashmaps" test is a lot slower, maybe someone can take a look at the source code, I use STL <map>, because <hash_map> was slower.... The memory footprint of Cpp is also the lowest, which was expected. C# is quit fast (I would have been surprised, if C# were much slower than Java), but seems to have some problems with exception handling, matrix multiply and nested loops. Java's hash maps are very fast and the exception handling is also very strong. If you sum it up, Java gets 5 wins against C#, C# gets 9 wins against Java and Cpp gets 11 wins against C#. This black and white comparison only takes performance into account, but other values like development speed and security are also very important issues today. In my opinion it goes much faster to develop complex apps with Java or C#, than with plain Cpp and the STL. Type safety and buffer overrun issues are also much weaker in Cpp, so you have to do more work, which takes more time and will cost some speed! So every language has its strong areas, but as always, nothing comes for free...


Update 15.04.04

Java Version + Options
  • Java 1.5 (alpha, 11. Dec. 03)
  • compiler options: -g:none
  • running options: -mx1024m -Xbatch and -server for the server VM
C# Version + Options
  • .Net Framework 1.1.4322 (Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4)
  • compiler options: /optimize+ /debug- /checked-
  • running options:

C++ Version + Options
  • Microsoft Visual Studio Net 2003 (Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86)
  • compiler options:  /Op /Oy /O2 /Oi /Og /Ot /EHsc /arch:SSE
  • Intel(R) C++ Compiler for 32-bit applications, Version 8.0   Build 20031017Z
  • compiler options:  /Op /Oy /Qpc80 /fast /G6 /Oi /Og /Ot /EHsc /arch:SSE
  • running options:

Source Code Changes
C++ :SLT VECTOR was replaced with the STL LIST, that's why the benchmark is now called "List". STDIO(C) was replaced with FSTREAM(C++).
Java :STRINGBUFFER was replaced with STRINGBUILDER, timeing routines changes, "new Integer()" was replaced with Integer.valueOf(), VECTOR was replaced with LINKEDLIST, so all "List" benchmarks use now a list, only C# uses its ARRAYLIST, because there is no list or linked list in C# and thats the reason, why these benchmark is marked red!

Results
Maximum memory usage:
  • Java - 163 MB
  • C# - 111 MB
  • Cpp - 98 MB
Performance in ms:

Cpp is a little bit slower with more accurate floating point calculations and omitted frame pointers. The Java server VM is faster than the client VM, which was new to me, because I don't have that much Java experience. Trig. functions and maybe the memory footprint are the only weak points in Java today. The "hashmaps" benchmark uses a helper class in Java, to achive this high performance. If you don't use it, you will get 11000ms instead of 3640ms, but this optimisation isn't possible and necessary for the C++ and C# versions!


Update 17.04.04

Java Version + Options
  • Java 1.4.2_03
  • compiler options: -g:none
  • running options: -mx1024m -Xbatch and -server for the server VM
  • Excelsior JET 3.5

Results
Maximum memory usage:
  • Java - 163 MB
  • C# - 111 MB
  • Cpp - 98 MB
Performance in ms:

The server VM (1.4.2_03) is faster than the 1.5(alpha) server VM from above and it's always faster than its client version. The JET 3.5 code is faster than the client VM, but sometimes it is slower than the server VM. Trig. functions are as fast as the Cpp and the C# code, but they seems to use some lower precision for the sin, cos and tan functions.


Feedback
Please use this thread for feedback.