Dear all,
I'm trying to rewrite a Java program into C++.
The Java programm works fast but I hoped that C++ would even be
faster.
But that is not the case !
(to be complete: both the Java and C++ version of the program are
intented to use within Matlab)
I used as less 'new', 'delete' and 'delete[]' statements as possible.
As less pointers as possible.
I did use vectors, but no iterators. And I confirmed that there are no
memory leaks at all.
But still, the C++ version of my programm is slower than Java with a
factor 100 !!
I have used Visual C++ 6.0 and later on Visual Express 2008.
How is that possible ? And are there people who had the same probem ??
Kind regards,
Brey
The Netherlands
The sentence "Java faster than C++" doesn't even make sense. How did
one compare two languages? Did they mean it in a computational
theoretical way that the intrinsics of C++ is doomed to be slower than
that of Java, which has a mathematically sound proof? Or did they do a
statistical research over a large number of C++ code and Java code
that provide the same functionality?
If you didn't provide any scientifically convincing comparation of C++
and Java. You are probably better off saying something like this: "hey
guys, I wrote a piece of C++ code that does the same thing as the Java
code I wrote the other day. Guess what, my c++ program runs 100 times
slower than my Java program. How surprising!!"
To me, saying "Java faster than C++" based on someone's own very
limited experience is nothing more than a joke. As far as I can see,
it serves only to provide false hope for some and/or to intimidate
others.
In comp.lang.java.programmer, we treat the converse claim as little more
than trolling.
The real problem is that Java and C++ run on different principals, so
you can't truthfully say "I've written the exact same program in both
languages."
Even Hello World is different:
#include <iostream>
int main(int argc, char **argv) {
std::cout << "Hello World" << std::endl;
return 0;
}
---
class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello World");
}
}
---
Both of those programs have the same goal, but the process that happens
is far different.
For the C++ program, the C/C++ runtime is initialized, and it passes
control off to the main method, which executes a function (operator<< )
on a ostream object, and then executes another function (operator<< ) on
the return value. What those operators do, I don't entirely know or
care, as long as there effect matches my intention...
For the Java program, the JVM is initialized (which probably includes a
the C/C++ runtime being initialized), the system class loader loads the
system classes, and then loads the HelloWorld class. After initializing
the HelloWorld class, it looks for a static void main(String[] args)
method, and invokes it. This method access the System class, reads a
static field, and invokes a method on that object (which is of type
PrintStream).
So, the simplest programs do things very differently, even though they
look similar enough. You can't compare Oranges to Orangutans.