Java faster than C++ ?? Impossible !

B

brey_maastricht

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
 
B

Boris

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 !
[...]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 ??

Why shouldn't it be possible to develop a slow program in C++?

Boris
 
M

Mirco Wahab

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 !!

If somebody says "Java is faster than c++", this
usually implies that Java and C++ are not used as
intended, with opposite signs. Some sophisticated
Java-coding by some expert often beats a naive
C++ implementation that can't avoid the pitfalls
inevitably connected with such a complicated thing
(like C++).
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 ??

Maybe the Java source was designed by a really clever
programmer (who knows the VM's internals) and you
tried to translate this beast too directly to
C++? Who knows. What kind of code is it?

Regards

M.
 
C

Carlo Milanesi

(e-mail address removed) ha scritto:
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 !
How is that possible ? And are there people who had the same probem ??

A typical error is passing aggregate objects (in particular,
collections) by value instead of by reference.
It can have a huge impact.

In this wikibook you can find many advices to improve the performance of
your software: http://en.wikibooks.org/wiki/Optimizing_C++
 
H

huili80

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.
 
P

peter koch

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 ??

It is difficult to tell without you showing any code. But others have
pointed out two likely sources: pass by value (which might look
natural to a Java programmer), which can be a time-killer and running
in debug mode, which easily can give give a factor of 10. I remember
in my early days, where I accidentically passed a (large) structure by
value. This one function caused made the program go five times slower.
If there had been more functions like that, it is easy to imagine a
factor of 100 as a result.
That said, it very much depends on the program how much a speed-up,
you could expect going to C++. I would not be surprised if you had no
speed-up at all, and I would be surprised if you got a factor of much
more than 2 unless the Java program should start trashing.

/Peter
 
D

Daniel Pitts

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.
 
I

iiahmadabdullah

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


Hi,

As I understand the problem, I could make a little guess about the
cause:

1) MATLAB runs on Java platform. Therefore, native java programs might
interact with MATLAB in a much efficient way than C++ programs. Don't
know the internal mechanisms. Please be specific about waht do you
intend to do with MATLAB.

2) In java, you use pointers / referencing implicitely. But as you
say, you avoid new, delete[] stuff in C++. This might be one reason
for getting slower because of "passing by value's" in exchange of
large data objects.

3) Most part of the the java runtime is already loaded when MATLAB is
running. But not the runtime requirements of the C++ program; May be,
you have to load it for the first time and this might take time.

All the best.

II Ahmad Abdullah.
 
W

W Karas

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

I think there is at least a hypothetical possibility that interpreted
intermediate code could be faster than native code due to caching.
Seems like it might be possible that the intermediate code together
with the interpreter might fit in cache, whereas the corresponding
native code would not. In this situation, if the native code was
sufficiently lacking in locality, it might be slower than the
interpreted code. But this seems unlikely, and even more
unlikely to cause a slowdown by a factor of 100.
 
I

iiahmadabdullah

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


Hi, One more point.

The slow running of C++ might be due to its interaction with MATLAB.
It can also be assumed that it's MATLAB that makes it delaying or
delays in responding to your code and who knows how the interactions
are and how it executes your C++ code, unless u clearly specify? GIve
some more specific info about what u r trying to do with *MATLAB*.
NOTE: MATLAB IS A JAVA PROGRAM. NOT A NATIVE WIN32 OR ANY OF THAT SORT.
 
L

Lionel B

[...]
Hi, One more point.

The slow running of C++ might be due to its interaction with MATLAB. It
can also be assumed that it's MATLAB that makes it delaying or delays in
responding to your code

No, it can't be assumed. It should be tested for. My experience is that
Matlab actually incurs a very small overhead in executing external
(native) code. I suspect this is a red herring.
and who knows how the interactions are and how
it executes your C++ code, unless u clearly specify? GIve some more
specific info about what u r trying to do with *MATLAB*. NOTE: MATLAB IS
A JAVA PROGRAM. NOT A NATIVE WIN32 OR ANY OF THAT SORT.

<please don't shout; and drop the SMS-speak - thanks>

AFAIK it is the Matlab *GUI* that is implemented in Java; the
computational engine is almost certainly implemented in native code (e.g.
Matlab will use native BLAS/LAPACK library code for many matrix
computations).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top