PDA

View Full Version : Will Java ever be as efficient as C or C++?



dra
06-05-2005, 05:17 AM
Will Java code ever be as (or close to) efficient as C or C++? The reason I'm asking is because there's a small discussion about programs written in Java code vs. the same programs written in C++.

The discussion primarily focuses on low level socket-based programs.

SMurf
06-05-2005, 06:11 AM
I'd say it's extremely unlikely unless lots of CPUs start supporting Java instructions natively.

It's that simple: Java does not compile to machine code like C++ does, it compiles to intermediate code which then has to be interpreted into the host system's machine code. There is a computational cost to that. It could be argued that when we're cooking on 8GHz processors or whatever new CPU enhancement (e.g. multi-core) they come out with that this cost will become negligable, but it will always be there.

Java is for multi-platform support without knowing anything about each platform. That's all it's for.

Prelude
06-05-2005, 08:58 AM
>Will Java code ever be as (or close to) efficient as C or C++?
Not as long as you use the JVM, since there's a level of abstraction that naturally effects performance when compared to native machine code. But if you compile Java to machine code using JIT, its performance is closer to C++ than C++ites would like you to believe.

B0bDole
06-05-2005, 09:40 AM
I think when mac and *nix take a bigger share of the OS market Java will become a more important tool. Until then, I hate it.

[edit] Doesnt really answer the question, but yah..

Krak
06-05-2005, 11:21 AM
Probably not. One big thing they boast about for Java is the fact that the JVM makes it completely portable, but it also makes it not quite as low-level as C. It also doesn't give you the power to manage memory....which makes it weaker than C in my opinion. I still like how easy it is to program graphic applets with Java though.

bithub
06-05-2005, 11:29 AM
The discussion primarily focuses on low level socket-based programs.
Well the bottleneck for many socket-based programs is not CPU code execution, but rather socket IO. In that case, it can be said that java performance could be very close or equal to that of C++.

vasanth
06-06-2005, 02:49 AM
Java and C++ are intended for different kind of problem solving... so any comparrision between them would not be fair.

sand_man
06-06-2005, 02:55 AM
Java and C++ are intended for different kind of problem solving... so any comparrision between them would not be fair.

Yes but the same program can be coded in ANY language, just each in a different way.

nickname_changed
06-06-2005, 03:38 AM
Yes but the same program can be coded in ANY language, just each in a different way.

Write any program with a half decent looking UI in Java. It can't be done.

adrianxw
06-06-2005, 04:45 AM
Java and C++ are intended for different kind of problem solving... so any comparrision between them would not be fair.

Indeed so.

Nice to see you back vasanth.

Govtcheez
06-06-2005, 07:23 AM
Write any program with a half decent looking UI in Java. It can't be done.
What do you mean by "half decent looking"? IMO, a UI should be chiefly be easy to understand and clean. There's no reason you can't do that in Java.

treenef
06-06-2005, 10:11 AM
I thought java had reasonably good GUI's. I didn't realise they were that bad.
:confused:

Perspective
06-06-2005, 10:58 AM
Write any program with a half decent looking UI in Java. It can't be done.

Sure it can, just stay away from Swing and AWT.... There are more UI libraries than the default API's provide. SWT for example will give you a standard API to the native widget set on whatever platform you're using, the UI will look exactly the same as the same UI written in C/C++, python, perl, etc...

Although, I would have to agree that "It can't be done" by a person that can't open their mind beyond the common stereotypes.

prog-bman
06-06-2005, 09:15 PM
C++ rules :)

jverkoey
06-06-2005, 09:48 PM
How does Java compile its programs? I was doing a test today on my mather parser to see how fast it would run, so I did a basic 3+3 and ran that through the parser a million times....it took 574 milliseconds. I then did the same thing in Java thinking it would take between 10-200 milliseconds or something....but it ran in 0, just like C++.

Now, I know it's not *as* fast as C++, but how did they efficienize their parser so much?

bithub
06-06-2005, 11:29 PM
Now, I know it's not *as* fast as C++, but how did they efficienize their parser so much?
Java VMs have a JIT compiler which converts java bytecode to machine code. The machine code for frequently used parts of the application are saved so that it doesn't have to be re-interpreted. So if you have a java program in which 95% of the CPU usage is done in a small loop, that loop wont be re-interpreted each iteration. In that case, the java program will run just as fast as its C/C++ equivalent.


Write any program with a half decent looking UI in Java. It can't be done.
Completely false. Run the program Azureus, and tell me how its UI is worse than a UI written in any other language.

Brian
06-07-2005, 02:04 AM
Write any program with a half decent looking UI in Java. It can't be done.

The UI on Azureus looks pretty ........ing sweet in my humble opinion.

sand_man
06-07-2005, 06:11 AM
I use Azureus and yes the UI is pretty sweet but lucky for me I have the RAM to spare because she is a bit of a hog.

B0bDole
06-07-2005, 06:29 AM
Azureus on linux doesnt hog anything, ???? hmmmm

nickname_changed
06-07-2005, 07:26 AM
You mean this Azureus? http://azureus.sourceforge.net/screenshots_v2.php

Hmmm.... the icons are nice, from the KDE Crystal theme, it certainly looks good for an open source project. The actual widgets themselves don't look anything special though, certainly nothing like Office 2003 (and yes you can make Office 2003 looking applications pretty easily in other languages).

Govtcheez
06-07-2005, 08:26 AM
So making a half decent UI means it has to look like Office 2003?

There is absolutely nothing wrong with Azureus's UI.

Perspective
06-07-2005, 09:00 AM
The UI on Azureus looks pretty ........ing sweet in my humble opinion.

A fine example of SWT hard at work :)

Jez
06-07-2005, 09:26 AM
> Now, I know it's not *as* fast as C++, but how did they efficienize their parser so much?

It may be that the Java compiler looks at your code, sees it does nothing at all a million times, and so does nothing. I know some compilers can eliminate null expressions like that.

I would suggest try looping say 1000 000 000 times, see if it still takes no time...

Or conversely change the code so the compiler must calculate something to return a result.

I'm guessing, of course, because I haven't seen your code. (It wouldn't help much if I did, since I don't know Java. ( I did try learning it once, but really had trouble getting to grips with it.))

Brian
06-07-2005, 10:09 AM
If you want to argue the weaknesses of Java you should be focusing on real-time performance not GUIs.

B0bDole
06-07-2005, 12:30 PM
I like guis in java personally, at least from a programming POV

okinrus
06-08-2005, 09:37 PM
Java VMs have a JIT compiler which converts java bytecode to machine code. The machine code for frequently used parts of the application are saved so that it doesn't have to be re-interpreted. So if you have a java program in which 95% of the CPU usage is done in a small loop, that loop wont be re-interpreted each iteration. In that case, the java program will run just as fast as its C/C++ equivalent.

The Java VM could dynamically optimize the heavely used portions. For example, the Java VM could optimize a section of code to perform fast with respect to one program configuation, then reoptimize the code. I don't see any reason why a C++ compiler with priofiling information couldn't do this, but the Java VM ought to be able to outperform most C++ compilers.

VirtualAce
06-09-2005, 07:43 AM
With Apple changing over to a hybrid version of Intel x86, Sun is going to have to come up with a much better reason for using Java other than just cross-platform compatibility. They will either have to speed it up by dumping the VM or die. And if they do dump the VM and yet the syntax is so close to being C++ like, and C++ is still faster...and there is no longer any portability issues.......why use Java for anything but web-apps. C++ can do low-level sockets just as well if not better than Java.


Will be interesting to see how these recent moves by Apple affect the whole issue.

In fact it seems this very topic is popping up on forums across the net. Some say its good for Java and some say its not.

And Apple is going to be x86 - even if they protect them somehow we all know someone is going to hack it so that their x86 can run any x86 software. Besides why would you move to an architecture that is so widely supported by tons of software only to change the chip internally so that support is gone? I think they will be true x86, but again this puts a big question in my mind now? What is the purpose of Apple computers and why do we need them if they are just x86 like every other system out there on the PC market?

7smurfs
06-09-2005, 08:13 AM
Err...correct me if I'm wrong but even if apple does use a standard x86, aren't there still going to be compatibility problems from the OS and such?

Govtcheez
06-09-2005, 08:19 AM
Besides, why would Sun change their entire strategy because of the movement of a couple percent of the userbase? There are still plenty of people that use non-Intel cores.

B0bDole
06-09-2005, 08:19 AM
>Err...correct me if I'm wrong but even if apple does use a standard x86, aren't there still going to be compatibility problems from the OS and such?

exactly, I can write ONE program, with a gui, and whatever I want in Java, and it will work on any computer with any OS as long as they have the java runtime environment... Just because Apple is switching archs doesnt mean that a windows program will work for OSX...

Perspective
06-09-2005, 08:52 AM
JVMs provide architecture and platform portability. Having an x86 cpu doesn't mean macs can use the x86 JVMs.. .there's no such thing... JVMs need to be architecture and platform specific.

Jez
06-09-2005, 12:49 PM
At the moment a Java applet will run on windows or Linux on a PC , the fact that they're both x86 isn't the issue. Let's see you write c/c++ code and compile it so the binary runs on either platform. With Java the byte code is completely portable. In any case, there are plenty of smart phones + PDA's , web fridges, etc using ARMs or whatever. I don't think Apple's switch will ruin Java's appeal as a multi platform solution.

Dante Shamest
06-09-2005, 01:35 PM
Let's see you write c/c++ code and compile it so the binary runs on either platform.

Although C++ binaries are not cross-platform, using #defines and proper versioning techniques, the source code can be built on any platform. This is how Firefox (http://www.mozilla.org/products/firefox/) works.

In any case, Java binaries are not really cross-platform either. Java needs the JVM to be installed first. So if your definition of cross-platform means "just find a way to run it", then C++ can just use a binary emulator, like WINE (http://www.winehq.com/) to run on both platforms.


At the moment a Java applet will run on windows or Linux on a PC

Not necessarily. You need the JVM installed. And if the applet uses a graphics feature that only appears in the latest versions of the JDK, it won't work.


With Java the byte code is completely portable.

Really? Try running Java code on these platforms: Xbox/PS2/Gameboy Advance. It won't work out-of-the-box, you'll have to find some unofficial JVMs.