Skip to main content

Is C++ faster than java? And is a language really a language without its legacy?

1 reply [Last post]
xumeng12
Offline
Joined: 2011-02-22
Points: 0

One of my friend is in the learning process.he studied and programmed in Java from the beginning up until now is almost 2 years. And he is going to study C++ next, and he is interested in it because C++ is the language which is used primary in game industry, he also did a little research on C++ vs Java as well as other languages, and he read other threads in other forum about this subject. Usually, the conclusion is C++ faster in most cases, except for lab benchmarks. However, he tried a little program, and C++ (using std::cout from iostream) is slower than Java.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
geohut
Offline
Joined: 2011-03-20
Points: 0

Yes, C++ is generally faster than Java. When you write a C++ program, the program that you write is converted directly into binary instructions (0s and 1s) for the machine it's on, and these 0s and 1s can only be understood by a machine with (in some loose sense) the same type of circuitry as the machine the 0s and 1s were created for. Your program runs fast because all a computer has to do to run your program is hand the 0s and 1s to the circuitry, but you have to worry about someone else's computer not having the same circuitry and thus misinterpreting your sequence of 0s and 1s.
When you write Java code, on the other hand, such code is converted into an intermediate language that can be understood by every Java-enabled machine. This means that when you give someone else your program, you generally don't have to worry about the possibility of a machine with different circuitry misunderstanding or not being able to run your program, but at the same time, the machine running the program must be the one to take on the task of translating the intermediate format into 0s and 1s. In Java this happens on-the-fly, and because this is an extra thing that the machine running a program must do to execute the same program logic (it still has to have the circuitry execute the 0s and 1s after translating), the program runs slower because more has to happen for the same program logic to get executed.
it generally wouldn't be possible for a Java program to beat the performance of C/C++ in pure computational power. That's why the gaming industry uses C++. It is possible, however, for certain types of special operations (like input/output) to be faster in Java if Java has more efficient implementations of these, though that seems slightly unlikely to me.
Another important consideration is that Java's made great strides in recent years in terms of increasing performance, so the disparity between Java and C++ performance isn't as bad as it used to be.

Gurgadurgen
Offline
Joined: 2014-02-17
Points: 0

I agree with most of the points you made in this post, but there are a few key things I'd like to note about Java and C++ that you had a bit off:

Firstly, the compatibility of Java over C++ has nothing to do with circuitry. If you're capable of running an operating system that Java supports (most of which also support C++), the "circuitry" will not make a difference to the compatibility. The compatibility comes into play when you start considering operating systems, which are software, not hardware. Something that compiles and runs on a Windows machine may or may not run on a Mac or Linux machine, and likewise for anything compiled on a Mac or Linux machine working with the other two. Java will generally guarantee that it will at least TRY to run on the other two machines, and although Java has been improved to the point where it will run virtually the same (pun not intended) on any machine, there are still a few key differences with some minor, distant functions that may behave differently.

The other thing, and I know this is a bit of nitpicking, but C++ does not compile into binary (zeros and ones). It compiles into assembly, which is then compiled into binary. While you may think this is a very minor difference, it's not. Assembly code can actually be modified (relatively easily, in some cases, though it's a much more verbose and difficult system of languages to master), whereas attempting to modify any significant portion of pure binary from software used today would be one of the purest forms of masochism.

That being said, companies who are concerned with the speed at which a piece of software runs (eg. game developers) often value someone's capabilities with C++ AND assembly language quite highly, as being able to manipulate the resulting assembly code can help to give a game that extra oomph to get it to run just a wee bit faster at key points, or even to make it run at all.