Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 16th, 2006, 10:12 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Java slower than c++

Now i'm new to java just learning it in school right now and i want to know why is it slower than c++?? now with c++ you declare memory on the stack while in java i don't think you can. Is that the primary reasson that it is slower?

Has it to do with the fact that java is an interpreted language while c++ isn't. Please can someone explain this to me.

Sorry for the newb question.
Eric the Red is offline   Reply With Quote
Old Feb 17th, 2006, 12:26 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
It's mainly about Java being interpreted whereas C++ is compiled.
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 1:11 AM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
http://www.idiom.com/~zilla/Computer...benchmark.html
The : "Java is slow" - is becoming more and more an "urban mith" so to speak. Yes, an application starts slower with java, but from then one, the differences aren't that greate.
And with each new jdk version, things are getting faster.

http://www.javaworld.com/jw-02-1998/jw-02-jperf.html
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 17th, 2006, 1:22 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
Java being slower than C++ is no urban myth. There are some cases where well written Java code can out-perform well written C++ code, but they're exceptions rather than the rule. Things like JIT in Java can give better performance, if your code does things over and over, as your code is optimised to run faster when it is run more often, but YMMV as to what code it works well with and what gains you get because it relies on internals of the virtual machine.

The JVM (which interprets Java byte codes) is a performance bottleneck for a Java program, although techniques like JIT do improve things a lot but that depends on the implementation of the virtual machine. C++ does not have a virtual machine, so no such bottleneck exists --- the primary bottleneck on performance of a C++ program is always quality of the code and libraries the program used. Java still has that bottleneck. A high quality C++ compiler optimises for the target machine, which a Java compiler does not. JIT can achieve the same effect for Java (and potentially do a better job as it occurs at run time, so can optimise execution to address actual hotspots in code) but JIT functionality is at least as difficult to implement well as high quality compiler optimiser.
grumpy is offline   Reply With Quote
Old Feb 17th, 2006, 1:32 AM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
I stand somewhat corrected. I did find this humorous though:
Quote:
Originally Posted by first article
# there is a similar "garbage collection is slow" myth that persists despite decades of evidence to the contrary,
Quote:
Originally Posted by second article
Where Java is significantly slower than C++, it's due to Java's stringent security model or to garbage collection.
I'm slightly curious on the age of the second article however; they specify their tests as being done using VC++ 5.0 and Java 1.1.5, both of which are severely outdated by now, and I'm assuming that VC++ 5.0 was as bad if not worse than VC++ 6.0 at following the standard.

However, both articles were interesting, especially the first.
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 1:51 AM   #6
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Quote:
I'm slightly curious on the age of the second article however;
Inspecting the URL of the second article leads me to believe it was written in 1998
jaeusm is offline   Reply With Quote
Old Feb 17th, 2006, 9:53 AM   #7
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Java is compiled into machince language and then the JVM runs the machine code and executes the program, a lot of people say Java is slower than C++ but also its been proven that its actually if anything a little faster than C++, I've read a lot of documentations on Java and C++ and then average out on run-time!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 17th, 2006, 10:05 AM   #8
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
Actually isn't java compiled into java byte code and then interpreted by the jvm? I know there are compilers that convert it into native code, but I was under the impression that the jvm interprets the byte code, not native code.
hoffmandirt is offline   Reply With Quote
Old Feb 17th, 2006, 10:15 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by reggaeton_king
Java is compiled into machince language and then the JVM runs the machine code and executes the program, a lot of people say Java is slower than C++ but also its been proven that its actually if anything a little faster than C++, I've read a lot of documentations on Java and C++ and then average out on run-time!
This is incorrect, I'm afraid. Java compiles to Java bytecode, as hoffmandirt rightly points out. Java bytecode is architecture independant, and is interpreted by the JVM (Java Virtual Machine). C++, on the other hand, is compiled into native machine code, and can be executed without a virtual machine.

Java bytecode has the advantage of being platform independant. With Java, you don't have to worry what machine you're programming on, and you don't have to worry about library or DLL conflicts - if the JVM runs, your program will probably run, too.

C++ has the advantage of being faster, because it cuts out the JVM middleman, and in many cases more memory efficient, as JVM instances can't share libraries, IIRC, whilst C++ programs do this all the time.

Java isn't going to be faster than C++, because the JVM slows it down. Java may have some more efficiently programmed libraries than C++, but that's another story altogether. In general, Java is several times slower than C++.
Arevos is offline   Reply With Quote
Old Feb 17th, 2006, 1:23 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Funny how "urban myths" come about because a machine runs at 3 GHz instead of 2 MHz. Nothing real about that chit, of course.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC