This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Performance and the fly likes Why Java run more slowly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Why Java run more slowly" Watch "Why Java run more slowly" New topic
Author

Why Java run more slowly

Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
Hi,
In addition to the interpreted language, what are other reasons that cause Java run more slowly than C++?


Thanks,<br />Mike
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Mike Yu:
Hi,
In addition to the interpreted language, what are other reasons that cause Java run more slowly than C++?

The fact that Java is interpreted really is the reason Java runs more slowly than a fully compiled language than C++. The fact that each line must be interpreted and then executed makes the language inherently slower than a language that is fully compiled and requires no interpretation. Of course, this does provide Java applications with greater portability than those written in C++.
Corey


SCJP Tipline, etc.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Corey McGlone:

The fact that Java is interpreted really is the reason Java runs more slowly than a fully compiled language than C++. The fact that each line must be interpreted and then executed makes the language inherently slower than a language that is fully compiled and requires no interpretation. Of course, this does provide Java applications with greater portability than those written in C++.
Corey

In fact, that is *wrong*!
With current hotspot technology, java programs *get* compiled to native code when run in a modern runtime environment. As this compilation can be based on dynamic analysis, the optimization techniques can be even more effective than the techniques used by static compilers like for the C++ language.
What really makes java slower than C++ are the additional safety checks. For example in C++ you can read past the boundaries of an array, cast an object to an illegal class etc. without even noticing!
So Java is trading performance for safety (and lowered maintenance costs).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And to tell the truth I haven't seen any performance comparisons between a Java app and a C++ app that would make say that C++ is faster. I think Java is just as fast and sometimes faster that C++.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Moving this to Performance...


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Java certainly was slower in its earlier days - now it's seldom a problem, really. Java does tend to be slower than C++ as it starts up. But once the JVM is started and classes are loaded, it is often as fast, and sometimes faster than, C++. Results can vary substantially depending on what sort of application you're working with - but well-written programs can be much faster than poorly-written programs. This is equally true in any language, and tends to be a more significant factor than language choice when trying to figure out why a given program runs slowly.
[ May 17, 2002: Message edited by: Jim Yingst ]
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by Thomas Paul:
And to tell the truth I haven't seen any performance comparisons between a Java app and a C++ app that would make say that C++ is faster. I think Java is just as fast and sometimes faster that C++.

I would generally agree. For some scientific apps, with heavy number crunching, I think C/C++ is still a little faster; crypto is another example. You can match C only if you put a lot of effort into really optimzing your code. Generally I would still use C, or, more likely, use Java, but make native calls to C for the heavy math parts.
--Mark
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
My understanding is that Java is slower as comparision to C/C++. Reasons are being an interpreted language, safety issues etc.
I had read benchmarks comparing Java - MFC - GNU C++ - VB, Java didn't do very well and was last in most of the benchmarks. But I read this benchmark 2-3 years ago, so things should have improved by now . I don't remember the website where I read this bench mark, but it was a non-microsoft website for sure. If I am able to find it, will post the link again.
But that doesn't make Java any less of a development option. There are many scenarios where I would love to have Java as the solution rather then C/C++.


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by Gaurav Mantro:
My understanding is that Java is slower as comparision to C/C++. Reasons are being an interpreted language, safety issues etc.

As Ilja pointed out above, most Java programs are no longer intepreted, but rather compiles to native code. Hence there are asymptotically as fast as C/C++.
--Mark
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Just my two cents: last year I implemented a 'matlab' like engine, biased towards large matrix manipulations in Java; the whole thing was programmable, extensible etc. etc. It took me less than two months to implement the whole thing; granted I had done some prototypes and design before those two months.
My department was curious about how fast this thing would be (they were more than satisfied by its performance, but hey, the sky wasn't the limit, heaven could be feasible ) if it were written in C++. C++ happened to be the 'lingua franca' at this department.
I started rewriting the whole shebang in C++ ... I had to come up with a reference counted memory management scheme, I had to defer the dynamic class loading elegance to a silly dlopen()/dlsym() kind of mechanism etc. etc. After six! months the rewrite was complete. The original +- 300 KB of .class files were replaced by more than +- 900 KB of .so files (don't you love those multiple template instantiations all over the place) and the grand result was: The Java version turned out to be just a meager 20% slower than the brand new C++ version ... and that exercise took me six months ... *sigh*
kind regards
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Jos,are you going to maintain both code bases
[ June 16, 2002: Message edited by: Charlie Sturman ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Charlie Sturman:
Jos,are you going to maintain both code bases

No I'm not, the C++ stuff was an 'off trail experiment' so I plea incompentence in all possible means your honour!
kind regards
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Jos Horsmeier:

I started rewriting the whole shebang in C++ ... I had to come up with a reference counted memory management scheme, I had to defer the dynamic class loading elegance to a silly dlopen()/dlsym() kind of mechanism etc. etc. After six! months the rewrite was complete. The original +- 300 KB of .class files were replaced by more than +- 900 KB of .so files (don't you love those multiple template instantiations all over the place) and the grand result was: The Java version turned out to be just a meager 20% slower than the brand new C++ version ... and that exercise took me six months ... *sigh*
kind regards


That is beautiful. be nice to see the actual test results. Seems strange that Sun does not tout such things on the javasoft website since its the daily issue.
 
jQuery in Action, 2nd edition
 
subject: Why Java run more slowly
 
Similar Threads
Why and How Java is better than C#?
What is -server and -client option?
Please suggest me a methodology.
Applied Java
Are 2 arrays more costly than 1 in 2D?