• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java/C++ performance issues  RSS feed

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
One of the biggest argument c++ developers have against java is about performance.
Does anyone know some real and serious study (benchmarking or something like) about this?
Thanks.
 
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already under discussion in the advanced forum.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll chime in here, since that one seems more about VB than C++ and your question seems more sincere.

There have been articles with tests supposedly showing Java to be faster than C++. If you search slashdot.org, there was enlightened discussion there about the merits (or lack thereof) of these tests.

At my last contract, we wrote programs that had to handle data streams from markets such as the Nasdaq, NYSE, and smaller ones all over the world (did you know Beijing has an exchange?).

Because of the safety of the code and the ease of writing good code, we wrote almost all of our programs in Java. But the faster feeds (like Nasdaq) had to be coded in C or C++.

Moreover, there was discussion of converting more feeds to C++ because, with a zillion different feeds all running simultaneously, lowering overally cpu and memory demands had a bottom-line impact because it allowed code to run on less expensive hardware.

This is my opinion: all other things being equal, a well-written program coded in C++ is probably going to be more cpu and memory effecient than the same program in Java.

This is also my opinion: it is easier to code a well-written program in Java than in C++. There is considerable snobbery among C++ developers who believe that they are better than they really are. I've worked with people like this. During the interview, where they're grilling you and using terms like "best of the best" to describe their group, they act all high and mighty. They want to know if you can tell them about virtual destructors (unneeded in Java, btw). But once you're in, you get to find the memory leaks and worse in their poorly designed C++ code so the programs stay running for months on end without growing (due to leaks) or crashing (due to some poor pointer arithmetic).

Mark
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:
There have been articles with tests supposedly showing Java to be faster than C++. If you search slashdot.org, there was enlightened discussion there about the merits (or lack thereof) of these tests.


enlightened discussion? on slashdot? surely you jest, mr. Wuest!

but since you speak from experience, i'd like to ask a few questions... what part of the Java environment was slowing down the applications you mentioned? was this an issue of JVM efficiency, class library implementation, business-logic algorithm efficiency, or something else? could any parts of it have been resolved by moving the resource-intensive parts to a C or C++ library accessed through JNI, or would that not have made enough of a difference, do you think?
[ May 26, 2005: Message edited by: M Beck ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark says:

This is my opinion: all other things being equal, a well-written program coded in C++ is probably going to be more cpu and memory effecient than the same program in Java.


I don't think there's any doubt that, in the limit, C/C++ programs will always win on code size. If you don't use virtual functions, and don't use dynamic memory allocation, C++ objects are only as big as their data members, no larger -- whereas Java objects are always larger.

But as far as performance goes, first of all, very few programs are CPU bound. Many are I/O bound, and many spend their time waiting for a user to click something. Some traditional Java I/O operations can indeed be slow. You can use java.nio these days to fix these problems as needed. For the most part, these kinds of programs are indistinguishable, performance wise, from native code.

Now, some programs are CPU bound. Whether you can rewrite a given program faster as native code depends on a number of factors. Array operations are intrinsically slower in Java, due to bounds checking. And if you really write native code with no virtual methods and no dynamic memory allocation, then you can remove a level of indirection that again, can give the edge to C++. {*}

But the final results depend on the implementation. What I said in the above paragraph holds only if the C++ compiler is as good at optimizing as HotSpot is. The truth is that the most popular Windows compiler (VC++) is simply atrocious. Benchmark after benchmark have shown that HotSpot beats VC++ compiled code every time. GCC isn't much better. There are heavy-duty commercial compilers that do far better, but they're not as widely used.

My bottom line is that nothing beats measurement. Don't believe anybody who posts here, including me. If your Java code is too slow, and you know there's no way to write it faster in Java, then go ahead and try to do better in native code. You may find out that you can -- but you may find out that you can't.

{*} Edit: I said "Java" here by accident!
[ May 26, 2005: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
My bottom line is that nothing beats measurement. Don't believe anybody who posts here, including me. If your Java code is too slow, and you know there's no way to write it faster in Java, then go ahead and try to do better in native code. You may find out that you can -- but you may find out that you can't.


Very well said. I wish this could be the final word anyone ever says on the topic. Sadly, I know it won't be.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what we did was to take a Java program that was handling a data feed that was getting faster and faster and re-wrote it, pretty much as-is, in C++. Yes, some things were done differently (i.e.: several timers have to be set which are implemented easily with signals in C++).

The data feed is multicast so, if you miss something, it's gone forever.

We used gcc, which is actually a quite good optimizing compiler. I was surprised to see a comment from someone who is under a different impression.

cpu and ram usage dropped 75%, IIRC.

I actually thought about doing a hybrid with jni (for some reason, I think jni is one of the coolest things about java, cooler than ejb's even ). But that adds a level of complexity where the result is harder to maintain than something written in Plain C++.

It has been awhile since I spoke with anyone there, so I don't know if they are still considering changing it all to C++. Most of these feeds (there are a couple of hundred different markets worldwide and, as an example, the Minneapolis Grain Exchange gets just a few small updates every hour) are so slow that there's no incentive to change them. I.e.: while they are using memory, most of it is swapped out and unused and they aren't burning much cpu. OTOH, the ones that start picking up speed seem to grow quickly. Before changing the one I referred to above, we had it running on new, fast, 4-cpu Dell servers and it clearly was going to be in trouble if they didn't either split the feed between two boxes and load balance externally or find some other way. Everyone was relieved to see the improvements in the C++ version.

Mark
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

If you think JNI and EJB's are the cool aspects of Java, I'd question much of anything you say.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
If you think JNI and EJB's are the cool aspects of Java, I'd question much of anything you say.


My younger brother ports Linux kernels to various architectures and writes Linux device drivers. When I "Fell In Love" with Java, well, in his mind I had been seduced by the Dark Side. I proved I could still do Anything I Want with JNI and then I took a standalone C program he had and made a servlet out of it and I was OK again.



These days, I've been writing little helper session beans for my new co-workers and they act like I'd hung the moon, so I decided EJB's must be cool if they make everyone like me so much!

Mark
[ May 26, 2005: Message edited by: Mark Wuest ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been seduced by the Dark Side! EJB's are pure evil.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woohh.
I'm happy for your replies. It's really usefull for me, thanks.
BTW, I'd say that entity EJBs are evil (not session EJBs).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!