Fraunhofer Austria Research GmbH
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
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)
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
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
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
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
How can i make develop in the following euclidean hash class in java?
Amal Hashim
in the following code in euclidean hash some of the result of the rand.nextGaussian() implementation is negative when set dimension 100 and w is 2. which affect in the hash method.
public EuclideanHash(int dimensions,int w){
Random rand = new Random();
this.w = w;
this.offset = rand.nextInt(w);
randomProjection = new Vector(dimensions);
for(int d=0; d<dimensions; d++) {
//mean 0
//standard deviation 1.0
double val = rand.nextGaussian();
randomProjection.set(d, val);
}
use the implement of the previous constructor in the following method.
public int hash(Vector vector){
double hashValue = (vector.dot(randomProjection)+offset)/Double.valueOf(w);
return (int) Math.round(hashValue);
}
but i want to use the result of hash method as index of dimension in bloom filter . how to solve this problem. as i can not use this negative result as index of bloom filter.
Related Publications
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...
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 ...
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...