Question
Asked 22 May 2014

Is recursive implementation faster in Java than in C++ ?

Is recursive implementation faster in Java than in C++ ? For instance; in the implementation recursive functions for Fibonacci sequences or that of Towers of Hanoi, which of the programming languages will run faster and under the shortest possible time? Java or C++ ?

Most recent answer

Torsten Ullrich
Fraunhofer Austria Research GmbH
Hi,
the problem of this question (and with this discussion) is that many people have a lot of assumptions, which are not stated explicitly. For example, to some persons only the language Java is called Java. Other persons call the complete pipeline of Java Language, Java Compiler, Java Virtual Machine and Java Runtime as Java. Therefore, the question should be more specific. If you are only talking about the language, the question is not very reasonable. A language does not have a speed and, consequently, cannot be faster or slower than sth. else. If you talk about a specific pipeline, then you should define it (which compiler?, which tool chain?). If you don't define the tools you want to compare, the result can be anything: for instance, use the LLVM framework. It has a frontend for C++ and Java, so that it should be possible to get the same intermediate representation. Then you can combine these interrmediate representations with a slower, non-optimized backend and wirh a faster optimized backend in any combination you want, which makes this benchmark obsolete.
Best wishes, Torsten.
2 Recommendations

All Answers (17)

Samir Amir
Elkem AS
Peter T. Breuer Is right
Adriel M. Ziesemer Jr.
Rio Grande do Sul Federal Institute of Education, Science and Technology
Use -O3 and everything will always be faster in C++, at least for an experienced programmer.
Use these cheats to optimize your code as you go:
But don't try to optimize everything or your productivity, your code legibility, and you mental heath will suffer.
1 Recommendation
Philon Nguyen
Concordia University
My experience with java and recursion is that you will most likely run into a "OutOfMemoryError: Java Heap Space" error very quickly.
The documentation says that you can fix this by allocating more memory to the heap. But back in the days of 32-bit OS's, this never really worked for me (I used to use the Sun Open Source version). I didn't take the time to try for the 64-bit versions...
That is why I always convert my recursive functions into an iterative function. The code is not as elegant, but works better.
1 Recommendation
Ali adil Yassin
University of Basrah
Recursive Implementation in C++ considers faster than Java.
It's very hard give another idea when you read the Peter T Breuer explanation. I also consider that C++ is faster than java.
Best regards
Torsten Ullrich
Fraunhofer Austria Research GmbH
Hi,
the problem of this question (and with this discussion) is that many people have a lot of assumptions, which are not stated explicitly. For example, to some persons only the language Java is called Java. Other persons call the complete pipeline of Java Language, Java Compiler, Java Virtual Machine and Java Runtime as Java. Therefore, the question should be more specific. If you are only talking about the language, the question is not very reasonable. A language does not have a speed and, consequently, cannot be faster or slower than sth. else. If you talk about a specific pipeline, then you should define it (which compiler?, which tool chain?). If you don't define the tools you want to compare, the result can be anything: for instance, use the LLVM framework. It has a frontend for C++ and Java, so that it should be possible to get the same intermediate representation. Then you can combine these interrmediate representations with a slower, non-optimized backend and wirh a faster optimized backend in any combination you want, which makes this benchmark obsolete.
Best wishes, Torsten.
2 Recommendations

Similar questions and discussions

Related Publications

Article
449 pages Das Studienprojekt Fahrplanoptimierung findet im Rahmen des Studiengangs Softwaretechnik statt. Die Studenten des Studienganges Softwaretechnik müssen im Laufe ihres Studiums 3 Studienprojekte von je 2 Semestern absolvieren. Dabei sollen sie lernen, wie man in einer Gruppe professionelle Software entwickelt. Der Schwerpunkt wird darauf ge...
Article
FASE (pronounced like "phase"), the Forum for Advancing Software engineering Education, was started in 1991 by members of the software engineering education community in order to have a electronic forum for the dissemination and discussion of events related ...
Book
Das Buch bietet eine verständliche, kompakte Einführung in die Softwareentwicklung mit Java. Es konzentriert sich dabei mehr auf Konzepte und Methoden als auf versionsspezifische Sprachelemente. Ziel ist die Motivation und Ausbildung zum Selbststudium. Die Konzepte werden durch die Realisierung von Projekten vermittelt. Dabei setzt dieses Buch auf...
Got a technical question?
Get high-quality answers from experts.