By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

Java faster than C++ ?? Impossible !

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Sun, 22 Jun 2008 14:09:13 +0200, <br*************@hotmail.comwrote:
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
Jun 27 '08 #2

P: n/a
br*************@hotmail.com wrote:
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.
Jun 27 '08 #3

P: n/a
Hi,

I assume you did a release build in VC++ to compare it with java? A debug
build is indeed quite slow

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

<br*************@hotmail.comwrote in message
news:e3**********************************@z72g2000 hsb.googlegroups.com...
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

Jun 27 '08 #4

P: n/a
br*************@hotmail.com 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%2B%2B

--
Carlo Milanesi
http://digilander.libero.it/carlmila
Jun 27 '08 #5

P: n/a
br*************@hotmail.com wrote:
But still, the C++ version of my programm is slower than Java with a
factor 100 !!
That's what happens when you compile a debug version.
Jun 27 '08 #6

P: n/a
On Jun 22, 8:09*am, brey_maastri...@hotmail.com wrote:
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.
Jun 27 '08 #7

P: n/a
On 22 Jun., 14:09, brey_maastri...@hotmail.com wrote:
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
Jun 27 '08 #8

P: n/a
hu*****@gmail.com wrote:
On Jun 22, 8:09 am, brey_maastri...@hotmail.com wrote:
>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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #9

P: n/a
On Jun 22, 10:01*pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
huil...@gmail.com wrote:
On Jun 22, 8:09 am, brey_maastri...@hotmail.com wrote:
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.
Jun 27 '08 #10

P: n/a
On Jun 22, 8:09 am, brey_maastri...@hotmail.com wrote:
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.
Jun 27 '08 #11

P: n/a
On Jun 22, 5:09*pm, brey_maastri...@hotmail.com wrote:
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.
Jun 27 '08 #12

P: n/a
On Mon, 23 Jun 2008 23:55:38 -0700, iiahmadabdullah wrote:
On Jun 22, 5:09*pm, brey_maastri...@hotmail.com wrote:
>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)
[...]
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).

--
Lionel B
Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.