Originally Posted by
Laserve
In general, C++ is faster than java. Sure you can do contests with average C++ coders against experienced java coders and the java coders would win.
You need experience to write fast code in Java,
you need experience to write correct code in C++
C++
Code:
real 0m0.023s
user 0m0.002s
sys 0m0.002s
Java (v1.5.0)
Code:
real 0m0.313s
user 0m0.138s
sys 0m0.030s
Code:
#include <iostream>
using namespace std;
int main()
{
for( int i = 0; i < 1000; i++ )
{
if( i % 2 == 0 )
{
cout << i << " is an even number! " << endl;
}
}
return 0;
}
final class even_numbers
{
public static void main( String[] args )
{
for( int i = 0; i < 1000; i++ )
{
if( i % 2 == 0 )
{
System.out.println( i + " is an even number! " );
}
}
}
};
C++ blows Java away in every single execution speed category where the programmer isn't totally STUPID.
Where Java blows C++ away is that in Java, there is a very comprehensive API for doing many, many things (and very quickly--in terms of coding them) compared to the rather limited "standard library" that comes with C++.
For example, if you wanted to write a network-centric app in C++ that goes out, grabs a web page contents and displays it--all starting from only what the standard library provides--you would code away for a month of Sundays. In Java, you could write it in about 8 lines of code...inside of 10 minutes all with just the "standard library" that ships with the JDK.
However, after your months of toil with C++, the execution speed would still be very much faster than it would be within the Java environment, assuming that the programmer wasn't a total moron.
However, bobthebullet990, the prinicpal argument that I'd use in my paper wouldn't be execution speed, it would be that C++ gives you low level access to the hardware whereas Java provides it solely through the JNI, which is then implemented in (usually) C code.
And, for anyone who wants to continue the argument of which language is faster/slower/better/worse, just remember that the JVM is written in C!
:davis: