Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java faster than C++ ???!!!  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm writing a paper for a computer science class that involves measuring execution times for various programming languages. Much to my suprise, Java (JDK 1.6, and even 1.4) is significantly faster than C++ in most benchmarks that I have measured. This outcome is contrary to what I have been lead to believe about the performance of the two languages. This leads me to believe that I have overlooked something in my testing methodology: for Java: using NetBeans to execute on JDK 1.6, source format 6. For C++: using Bloodshed DevC++, 'highest optimization' under 'options'. The programs are logically as similar as possible, given slight syntactic differences between the languages. Execution time is measured: for Java:

long firstTime = System.CurrentTimeMillis();
//code I wish to measure here
long difference = System.CurrentTimeMillis()-first;
System.out.println(long);

And for C++:
int firstTime = clock();
//code to measure
int diff = (clock()-firstTime);

I've measured execution with a stopwatch, and for a Java program that I timed at 23.5 seconds, execution time was 22542 ms, and I got a similar result with the C++ compiler, so the system clock for both seems to be accurate. Is Java indeed all-around faster to execute than a comparable C++ program? I measured: loops, Fibonacci, random number generation (C++ was considerably faster here), string performance (again, C++ faster here), STL sorts, method calls with different return types, and user-defined object creation. Java exceled in all areas with the noted exceptions above. Anyways, if anyone knowledgeable enough sees any flaws in my approach (or if it looks fine and legitimate to you), I would greatly appreciate a response.

Thanks in advance!
 
Henry Wong
author
Sheriff
Posts: 22830
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, measuring Java against C++ is like comparing apples and oranges. Not only are the compilers different. Not only is the runtime environment different. But the program is complete different as well. It is really difficult to draw any major conclusions with these tests...

But yes, Java does beat C++ is many cases. Java is also slower than C++ in many cases. It is very dependent on the applications being tested. But there may be a few reasons why Java does beat C++ in certain cases...

For C++: using Bloodshed DevC++, 'highest optimization' under 'options'.


Some C++ compilers support a profiling mode. Basically, the C++ compiler generates an instrumented application. This application actually runs slower, but generates a huge amount of logging data. At a later time, the C++ compiler is used to compile the program again, using the profiling data that was generated from an earlier run.

This mode easily beats even the "highest optimation" option of the compiler.

Java is similar in this regard. Since, it uses Just-in-time compilation, it will profile the java class initially, and then compile the class, in an optimized fashion, using the profile data that it collected.


Another reason may be the popularity of the language. Java's usage for applications is currently much higher than C++ usage. Java also have more vendors trying to out-do each other with optimized libraries. As such, the applications, and the libraries that they use may be better written.

Henry
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I was afraid of, many factors contributing to the outcomes. I suppose I'll repeat with different compilers and hope that a discernible pattern presents itself. Thanks for the explanation, and your Java Threads book has helped me with that confusing subject.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!