473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java faster than C++ ?? Impossible !

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
12 1932
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Wolfgang Keller | last post by:
Hello, as a non-developer I am currently participating in an industrial "research" project to develop a so-called "web application". This application serves at the same time as middleware to...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
19
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
15
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
148
by: BillJosephson | last post by:
Want to do OOP. Does c++ have all the abilities of java, or is it some subset? Thanks...
358
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.