views:

620

answers:

10

Is it possible for a Java application to be faster than a program written in C++? Also, what is release mode in compilation ?

+6  A: 

I think there are plenty of questions you can browse through that are similar. Have a look here, for example: C++ Performance vs. Java/C#

schnaader
A: 

Yes it can. The JIT compiler can optmize the code to run faster. While in C++ you have to do that optmization by hand.

ericmj
Not necessarily. Java being in some ways a simpler and more restrictive language helps the JITter and compiler optimize better, but C++ compilers can also do a lot of optimization for you without your explicit intervention (through pragmas or code tuning).
crosstalk
Naive answer to a silly question.
Martin York
-1 This is nonsense, basically. Java code will only run faster than C++ code if it's written worse (eg bubble-sorting a million items in C++ vs a Quicksort in Java). This myth was popular about 5-10 years ago but has long since been debunked.
cletus
It's not nonsense. He said *can*, not "will always be the case. A JIT compiler could *in theory* perform optimizations far beyond what's possible at compile-time. They don't do this in practice, but it's possible.
jalf
We yet haven't seen a java or .net JIT that uses SSE or MMX (or altivec) to accelerate bulk data processing. The hotspot JITs could in theory outperform traditional (static) compilers, in practice they don't.
Nils Pipenbrinck
I'm deep in the C++ camp myself (don't know Java), but if the JIT can optimize based on the data it is seeing and the code paths it is taking (as I understand it does), then I say it is true. +1.
Mark Santesson
+2  A: 

Yes it can: Make a Java program that uses a good algorithm, and a C++ one that uses a bad one and the Java version will likely be faster.

Nemanja Trifunovic
maybe on the second run. the first time, C++ program has time for a billion inner loops while the JVM is loading itself from disk.
amwinter
+7  A: 

Languages don't have a speed. A good Java compiler may generate more efficient code than a bad C++ compiler, and vice versa. So which one is "fastest"?

Execution speed depends on several factors:

  • The compiler. Different compilers generate different output code from the same input.
  • Your source code. Some operations are cheap in one language, but expensive in others. allocating memory with 'new' is far slower in C++ than in a managed language like C# or Java, for example)
  • The system it's running on. (CPU's vary in how fast they can execute different code. What if your Java compiler turns out to generate code that runs really well on a Core 2, but my C++ compiler generates code that runs well on a Phenom? Which is fastest then?

But the language is in principle irrelevant. Each language enforces certain guarantees which, might prevent certain optimizations. But a clever compiler may determine that these guarantees can be safely bypassed in this particular case, making the optimization anyway. (For example, a Java compiler will often try to eliminate the bounds-checking that is required by the language). So it depends on the code you're testing (Java code ported to C++ will probably run better in the Java version, and vice versa), and on how you compile it and where you run it.

So as Martin York says, it's a silly question. It's impossible to answer. Of course Java can be faster than C++ in some situations. For example, if you write really good Java code and really bad C++ code. Or if you use a lousy C++ compiler. Or if any of a million other things just so happen to favor the Java version a bit.

Say after me: Languages don't have a speed.

Which is fastest? English or french? Both are just ways to associate meaning with sounds, or squiggles on paper.

The same applies to programming languages. A programming language is just a way to associate semantics with a sequence of characters in one or more files.

jalf
Languages don't have speed but language specifications do affect the implementations' ability to produce optimized code. For instance in case of an array index out of bound, Java is required to throw an exception, while C++ is free to do nothing.
Nemanja Trifunovic
A: 

If I believe what some Java evangelist say, I would answer "yes" to the first question. Ie. a Java program "can" be faster. Not always, though...
These Java evangelists point out the better memory management, avoiding overhead of new, etc.

To answer the second question, release mode for a C/C++ compiler means a compilation without debugging information: the latter stores additional information like line numbers corresponding to generated code (easier for debugging and error reporting), and avoids optimizations (which can change code order and mess with above info).
So release mode is, in general, faster and smaller. And can crash where the debug mode works! (Rare but I saw that.)

PhiLho
I just want to point out that if your release build crashes, in 99% of all cases it's the programmers fault (e.g. usage of an uninitialized variable or violating the c/c++ standard in a way or another). Compiler-Bugs happen but are *very* rare.
Nils Pipenbrinck
+3  A: 

Unlike some people here, I'm going to be reasonable and answer the question that I know the OP intended to ask, even though his statement of it could have been better.

Yes, a typical java implementation can be faster than a typical C++ implementation for real-world things. Even though Java has a few handicaps from being a safe, VM language, it makes up for some of them, too.

For one thing, since Java has a very abstract memory management scheme that does not allow raw pointers or untyped memory blocks to be manipulated, it can use a moving garbage collector. In C++, untyped memory regions, unions, etc. would break this. Therefore, when a GC does not need to be run, allocations in Java can simply be a pointer bump. There is no practical way to do this in C++, since a fully moving GC cannot be implemented in a language that supports the kind of low level manipulations that C++ does.

Furthermore, the VM of a typical implementation of Java is a double-edged sword relative to the statically compiled typical C++ implementation. The VM has some inherent overhead, but also allows some extra optimizations. For example, let's say you have some kind of virtual function like (Please excuse incorrect syntax, as I don't regularly use Java):

abstract class Foo {
    void stuff() {}
}

class Foo1 extends Foo {
    void stuff()  {
        System.out.println("Foo1");
    }
}

class Foo2 extends Foo {
    void stuff()  {
        System.out.println("Foo2");
    }
}

// Somewhere in program initialization:
Foo foo;
if(args[0] == "Foo1")
    foo = new Foo1();
 else foo = new Foo2;
 for(int i = 0; i < 1000000; i++)
     foo.stuff();

In C++, a virtual function call on foo.stuff() would likely have to be performed for all 1,000,000 iterations. In Java, the VM might be able to replace this with a direct call at runtime, after realizing that there is no semantically legal way for foo to be rebound to an object of class Foo2.

dsimcha
A: 

Release Mode means that you build your program because you want to release it out to the public. Usually, the compiler will try harder to make the executable smaller and faster. This often means getting rid of symbol information needed get a backtrace of a crash and using a higher optimize level. The latter makes compilation time slower, so it's not used when building in Debug Mode.

Johannes Schaub - litb
A: 
rjoshi
A: 

Most discussion compare applications compiled from source. However if you have an old library e.g. from a third party, you can find that an old java library is much faster as it will still be able to use the latest instructions/techniques whereas an old library already compiled to native code cannot.

Peter Lawrey
A: 

I guess the most interesting thing you can say on this topic is that a modern JVM does some speculative optimizations. Optimizations based on guesses about the stableness of some values, whith the possibility to unoptimize that code if the value should change in the future.

OTOH this is nothing inherent with the languages in question. I saw a research project a couple of years ago that managed to get an extra 20% out of some natively compiled code (maybe even from C++) by running it in an emulator emulating the same CPU as the hardware but performing some VM-style optimizations on the code.

John Nilsson