The difference is the presence of the JIT compiler at runtime. If you're talking about keeping some or all of the C++ compiler around while the program is running, then you're talking about a very unusual runtime environment.
If the JIT compiler really makes such a difference in performance as you think it does, don't you think that more of the top developers would use it? In your specific example, determining a loop invariant is not a free action. I hate to invoke Karmack here, but do you think that (just from a performance standpoint) Doom 3 could be written in Java?
If your answer is yes, you are really deluding yourself.
What you're basically saying is if I want to hand optimize every line of code I write, rather than concentrating on the task at hand, then I should use C++, but if I want the compiler to do the heavy lifting then I should use Java?
No, I'm not saying that at all. I'm saying Java can be faster, in some circumstances, if you are talking about a naive implementation. I never said that Java would always be faster, and I hardly think that is the case (some undergrad troll who made the front page of/. notwithstanding).
However, when you need the utmost in performance, and second best won't cut it (and assembly is too much hassle), c++ is your best choice. Do you think Doom 3 could possibly be written in java? I'll answer that one for you - no way in hell. To suggest otherwise is ignorant and ludicrous at best, trolling at worst.
Well, the guy who posted this article wasn't exactly Charles Darwin.
His idea of "optimized c++" is the -o2 switch in g++. Gimme a break!
He took some off the shelf code and called it "optimized".
His (and your) argument basically boils down to whether the naive approach in java can be faster than the naive approach in c++. Of course it can, but don't call it optimized.
I think some of you are overlooking the fact that a VM running byte code is capable of doing optimizations that a compiled language just can't possibly do.
Err.... what language is the JVM written in?
I'd guess it's written in c++. And if it isn't, you certainly could write it in c++, don't you agree?
Now, have you really thought this out?
No one is saying a naive implementation (like the ones this guy posted) cannot be faster in java than c++. However, when you say "optimized c++", that does not mean g++ with the -o2 switch. That means you optimized the code.
Any Java-specific advantages you can name could themselves be implemented in c++. Would it be a good idea to do so? Probably not. If the tool is the best for the task, use it. Just don't give us this crap about "optimized code", when all you mean is you told the compiler to do its best with a naive implementation.
If by "interpreted by the java virtual machine at runtime" you mean "compiled to machine code that runs natively", then yes.
Well, that is what I meant. I realize that "interpreted" is a slightly loaded term and it was not my intention to set off your buttons. The fact remains that it is done at runtime, and the java opcodes are not native opcodes on a general purpose cpu, like Intel or AMD. As I've had to iterate to several other posters, I am certainly aware that a naive implementation of an algorithm could be faster in java than c++. That, however, is beside the point. Anything clever that the JVM does which is so great, a skilled c++ technician could also do.
What language is the JVM written in, anyway? I don't know, but my guess would be c++.:)
If you designed hardware to run java, well then java is native code, and likely nothing will be faster than java on that platform. This is fairly obvious, and hopefully it was fairly obvious in my reply that I was talking about a general purpose computer.
If you mean hand-optimised, by a perfect developer with infinite time available to effectively generate his own run-time framework, then perhaps not.
Well, that is what I meant. I took issue with the idea that "optimized c++" means using the o2 switch in gcc.
I should have made it clear that naive implementations in java vs c++ can be faster. I considered this to be obvious, and not related to the question of whether optimized code is faster. It cannot be, and I think you will agree.
Does this mean it's always worth the effort? No, and emphatically no. Re-inventing the wheel is rarely a good thing, and if java comes with some kick ass runtime advantages out of the box that you would have to implement yourself in c++ to get the same performance, by all means, use java.
Suppose that during a particular run, there is a particular variable x that, whenever we look at it, always has the value 42. Now we want to compile a method that uses that value. A JIT can optimistically assume that value will always be 42. Now it can go to town on optimizations involving that variable. Got an "if(x > 10)" statement? You can omit the compare and branch.
Are you saying that cannot be done in c++?
Give me a break. It's called a constant. The precompiler replaces the tokens with constants before compilation. So in your example:
#define LifeTheUniverseAndEverything 42
if (x > 10) {...}
becomes
if (42 > 10)
before the compiler even gets involved in the picture. When a sensible compiler sees "if (42 > 10)", it will not create a branching statement.
I know my statement was pretty over the top. However, any Java advantages you can name could be themselves implemented in C++. Even if you are talking about self modifying code, that can be done in c++, it's just that usually self modifying code is not something you would do on purpose (although many people do it by accident:).
Now, that having been said, I am perfectly open to the possibility that a naive implementation in c++ will be slower than a naive implementation in java for a specific algorithm. But when you say "optimized", there's a certain level past which you cannot optimize in java, whereas you can get right down to the nuts and bolts in c++, with some compilers you can get right down to the registers without even writing assembly code! Java is an extra layer of cruft that keeps you from the machine details, which is its major appeal ! Use it where it makes sense, where you need good cross platform compatibility, or you feel the design constraints are better fit to the task, or whatever your judgement tells you, but not because a comp sci undergrad took some off the shelf code and proclaimed it faster.
At the end of the day, the real question that I should be asking myself is which language gives me the best balance of maintainability, ease of development and elegance of expression.
Well said! I completely agree. That's basically what I was saying in my first paragraph. Java doesn't have to be faster to be better for a given task. Perl is the best choice for some tasks, and we all know how slow Perl is.
However, when a computer science student takes some off the shelf code, and compiles it in Java and gcc, and comes to the conclusion that Java is faster, well, I just have to say something about it. I looked at his code, and the algorithms are implemented fairly naively. His idea of optimization is a *compiler switch*. Ha!
Java simply cannot be faster than highly optimized c++ (with a good compiler, of course), any more than a pig could fly if you gave it wings. I'm sure you know that as well as I do. There's no magic going on in that virtual machine. At best, highly optimized Java could equal highly optimized c++.
I'd like to see a C++ implementation of the Halting Problem
Eh.... so for your counter example you choose a problem that is unsolvable in any language?
Did you just read about that in your computer science class or something? How does that relate to the question at hand? For any java implementation, of any algorithm, I (or someone else) can write a c++ implementation that is faster. Does this mean that I can solve problems in c++ that are unsolvable? Ummm.... no.
This is a common logical fallacy. Its Latin name is "Non-Sequitur". You'll probably get to that next year when you study logic.
Java is not faster than optimized c++
on
Java Faster Than C++?
·
· Score: 3, Insightful
It's just not possible. It could be comparable, in limited cases, but not faster. It just can't be. If you find that it is, there's something wrong with your experiment. Does this mean Java is bad? Not necessarily. It depends on your purpose.
Okay, so how could I make a blanket statement like that? In this case, the author of the paper merely used a compiler switch in gcc (-o2). That doesn't mean his c++ was highly optimized. It just means he told the compiler to do its best. If you really wanted to highly optimize c++, you would study the compiler and how it works, and you would profile the actual assembly that the compiler generates to make sure that it didn't do anything unexpected. Given *any* algorithm, I can come up with a c++ implementation that is faster than a Java implementation. Period.
The java compiler actually compiles to a virtual opcode format, which is then interpreted by the java virtual machine at runtime. Imagine if you needed to talk to someone on the phone, but instead of talking to them, you had to talk through an intermediary. Is there any possible way that it could be faster than talking to the person directly?
Now, I'll be the first to point out that a badly implemented c++ algorithm could be much slower than a well implemented Java algorithm, but I'll take the pepsi challenge with well written code any time, and win.
Relying on benchmarks and code somebody else wrote doesn't prove anything. Did he get down and dirty with the compiler and look at the generated assembly code? No, he did not.
Well, okay, fair enough. I didn't just make it up though. I've read a couple of stories about bankrupt spammers, and also about "spam kings" that have their minions do a lot of the actual footwork, make all the money, and leave their mates hanging. It's also pretty common wisdom among the anti-spam activists that the spammers claims are wildly inflated. I mean, look at their sales pitches. Why would their self-promotion be any different?
He may be sorry, but I'm sure his bank account isn't...
Spammers and scammers like to brag about their income and exploits, but they're usually lying. Sure, a few people make money at it. My guess is though, that most people do not.
Re:Prime example
on
Meet Joe Blog
·
· Score: 1, Interesting
and this guy has ended up on the list of the 100 most influential people in Hollywood.
Yeah, because (sorry Harry, you live in my town and are good friends with one of my good friends, but) he likes EVERY DAMN MOVIE.
That is only a slight exaggeration. Of course you can become influential in Hollywood if you cover their latest crap-fest in icing and candy sprinkles. I even read aintitcool sometimes, but not for serious critique.
I've met Harry, and I think he genuinely does like a lot of movies, I don't think he's selling out to corporations or anything, he was doing this long before he got noticed. To put it kindly, though, his tastes are a bit, shall we say, broad?
I hate those damn commercials. So far, they haven't totally eroded my desire to go to the movie theater, but they're on track to do just that.
There may be a theater in your area that doesn't show ads. Look here
for more information.
Here in Austin, there's a place called the Alamo Drafthouse, and not only are they commercial free, they show cool/funny clips from old movies for the early birds. I refuse to watch movies anywhere else.
Just in the general case (disregarding this "proof"), what if a million dollar "proof" is published, is found to have a non-trivial error, and someone else fixes the error and publishes a very similar proof largely based on the incorrect one, which turns out to be correct? Who gets the money? Who decides the triviality of the error?
That is just too funny. Any bets how long it takes them to fix it? I keep getting this picture in my mind of the Seinfeld with Kramer and his little person friend, wearing the same shirt, and the little person says "We'll look like idiots!"
Not that great of a coincidence that Plato, who lived ~2500 years ago said Atlantis was ~9000 years before him?
That's 11,500 years ago. We have only recently discovered that the earth's temperature rose anywhere from 3-12 degrees in less than a decade almost exactly 11,500 years ago. Even if you say that it's not meaningful, it is a strong coincidence. Look up the word in the dictionary.
There's an old theory in paleoclimatology called "steady state theory" which holds that changes in the earth's climate happen very gradually, over hundreds of years. We know now beyond any reasonable doubt that that theory is false when it comes to temperature changes. Glacier core samples do not lie. Scientists have come up with all kinds of explanations for why nearly every culture has a global flood myth, including your river speculation, except for the obvious: there was a flood.
Paleoclimatologists and geologists are starting to come around to my way of thinking.
We've found several submerged cities in the past, right around the various coastlines. People like to live right on the coast. If the ocean level rose rapidly, it would kill off most of the earth's human population, and it would appear the whole earth had flooded (and it would appear a large island like Atlantis had sunk).
Why can't these people get it through their heads thaty Atlantis, as recounted by Plato in Timaeas and Critias, is allegorical.
That's just speculation on your part. It is a pretty interesting coincidence that almost exactly the time that Plato said Atlantis sank, we know the temperature of the Earth raised drastically. I can provide hard science to back this up if you don't believe me.
We also know that the ocean level was 300-500 feet lower before then. There is ongoing debate about how fast the ice melted and how fast the ocean level rose, but if it rose even 50 feet rapidly, a very large island (as Atlantis is purported to have been) would appear to sink almost overnight.
If it's an allegory, those facts make for a hell of a coincidence.
However....
The difference is the presence of the JIT compiler at runtime. If you're talking about keeping some or all of the C++ compiler around while the program is running, then you're talking about a very unusual runtime environment.
If the JIT compiler really makes such a difference in performance as you think it does, don't you think that more of the top developers would use it? In your specific example, determining a loop invariant is not a free action. I hate to invoke Karmack here, but do you think that (just from a performance standpoint) Doom 3 could be written in Java?
If your answer is yes, you are really deluding yourself.
No, I'm not saying that at all. I'm saying Java can be faster, in some circumstances, if you are talking about a naive implementation. I never said that Java would always be faster, and I hardly think that is the case (some undergrad troll who made the front page of /. notwithstanding).
However, when you need the utmost in performance, and second best won't cut it (and assembly is too much hassle), c++ is your best choice. Do you think Doom 3 could possibly be written in java? I'll answer that one for you - no way in hell. To suggest otherwise is ignorant and ludicrous at best, trolling at worst.
Well, the guy who posted this article wasn't exactly Charles Darwin.
His idea of "optimized c++" is the -o2 switch in g++. Gimme a break!
He took some off the shelf code and called it "optimized".
His (and your) argument basically boils down to whether the naive approach in java can be faster than the naive approach in c++. Of course it can, but don't call it optimized.
What language is the JVM for redhat written in?
Err.... what language is the JVM written in?
I'd guess it's written in c++. And if it isn't, you certainly could write it in c++, don't you agree?
Now, have you really thought this out?
No one is saying a naive implementation (like the ones this guy posted) cannot be faster in java than c++. However, when you say "optimized c++", that does not mean g++ with the -o2 switch. That means you optimized the code.
Any Java-specific advantages you can name could themselves be implemented in c++. Would it be a good idea to do so? Probably not. If the tool is the best for the task, use it. Just don't give us this crap about "optimized code", when all you mean is you told the compiler to do its best with a naive implementation.
Well, that is what I meant. I realize that "interpreted" is a slightly loaded term and it was not my intention to set off your buttons. The fact remains that it is done at runtime, and the java opcodes are not native opcodes on a general purpose cpu, like Intel or AMD. As I've had to iterate to several other posters, I am certainly aware that a naive implementation of an algorithm could be faster in java than c++. That, however, is beside the point. Anything clever that the JVM does which is so great, a skilled c++ technician could also do.
What language is the JVM written in, anyway? I don't know, but my guess would be c++. :)
If you designed hardware to run java, well then java is native code, and likely nothing will be faster than java on that platform. This is fairly obvious, and hopefully it was fairly obvious in my reply that I was talking about a general purpose computer.
Well, that is what I meant. I took issue with the idea that "optimized c++" means using the o2 switch in gcc.
I should have made it clear that naive implementations in java vs c++ can be faster. I considered this to be obvious, and not related to the question of whether optimized code is faster. It cannot be, and I think you will agree.
Does this mean it's always worth the effort? No, and emphatically no. Re-inventing the wheel is rarely a good thing, and if java comes with some kick ass runtime advantages out of the box that you would have to implement yourself in c++ to get the same performance, by all means, use java.
The author just got my hackles up a bit.
Eh... my example should read
if (LifeTheUniverseAndEverything > 10) {...}
But you (hopefully) get the point.
Are you saying that cannot be done in c++?
Give me a break. It's called a constant. The precompiler replaces the tokens with constants before compilation. So in your example:
#define LifeTheUniverseAndEverything 42
if (x > 10) {...}
becomes
if (42 > 10)
before the compiler even gets involved in the picture. When a sensible compiler sees "if (42 > 10)", it will not create a branching statement.
I know my statement was pretty over the top. However, any Java advantages you can name could be themselves implemented in C++. Even if you are talking about self modifying code, that can be done in c++, it's just that usually self modifying code is not something you would do on purpose (although many people do it by accident :).
Now, that having been said, I am perfectly open to the possibility that a naive implementation in c++ will be slower than a naive implementation in java for a specific algorithm. But when you say "optimized", there's a certain level past which you cannot optimize in java, whereas you can get right down to the nuts and bolts in c++, with some compilers you can get right down to the registers without even writing assembly code! Java is an extra layer of cruft that keeps you from the machine details, which is its major appeal ! Use it where it makes sense, where you need good cross platform compatibility, or you feel the design constraints are better fit to the task, or whatever your judgement tells you, but not because a comp sci undergrad took some off the shelf code and proclaimed it faster.
Well said! I completely agree. That's basically what I was saying in my first paragraph. Java doesn't have to be faster to be better for a given task. Perl is the best choice for some tasks, and we all know how slow Perl is.
However, when a computer science student takes some off the shelf code, and compiles it in Java and gcc, and comes to the conclusion that Java is faster, well, I just have to say something about it. I looked at his code, and the algorithms are implemented fairly naively. His idea of optimization is a *compiler switch*. Ha!
Java simply cannot be faster than highly optimized c++ (with a good compiler, of course), any more than a pig could fly if you gave it wings. I'm sure you know that as well as I do. There's no magic going on in that virtual machine. At best, highly optimized Java could equal highly optimized c++.
Eh.... so for your counter example you choose a problem that is unsolvable in any language?
Did you just read about that in your computer science class or something? How does that relate to the question at hand? For any java implementation, of any algorithm, I (or someone else) can write a c++ implementation that is faster. Does this mean that I can solve problems in c++ that are unsolvable? Ummm.... no.
This is a common logical fallacy. Its Latin name is "Non-Sequitur". You'll probably get to that next year when you study logic.
Okay, so how could I make a blanket statement like that? In this case, the author of the paper merely used a compiler switch in gcc (-o2). That doesn't mean his c++ was highly optimized. It just means he told the compiler to do its best. If you really wanted to highly optimize c++, you would study the compiler and how it works, and you would profile the actual assembly that the compiler generates to make sure that it didn't do anything unexpected. Given *any* algorithm, I can come up with a c++ implementation that is faster than a Java implementation. Period.
The java compiler actually compiles to a virtual opcode format, which is then interpreted by the java virtual machine at runtime. Imagine if you needed to talk to someone on the phone, but instead of talking to them, you had to talk through an intermediary. Is there any possible way that it could be faster than talking to the person directly?
Now, I'll be the first to point out that a badly implemented c++ algorithm could be much slower than a well implemented Java algorithm, but I'll take the pepsi challenge with well written code any time, and win.
Relying on benchmarks and code somebody else wrote doesn't prove anything. Did he get down and dirty with the compiler and look at the generated assembly code? No, he did not.
Move along, there's nothing to see here.
Well, okay, fair enough. I didn't just make it up though. I've read a couple of stories about bankrupt spammers, and also about "spam kings" that have their minions do a lot of the actual footwork, make all the money, and leave their mates hanging. It's also pretty common wisdom among the anti-spam activists that the spammers claims are wildly inflated. I mean, look at their sales pitches. Why would their self-promotion be any different?
Spammers and scammers like to brag about their income and exploits, but they're usually lying. Sure, a few people make money at it. My guess is though, that most people do not.
Yeah, because (sorry Harry, you live in my town and are good friends with one of my good friends, but) he likes EVERY DAMN MOVIE.
That is only a slight exaggeration. Of course you can become influential in Hollywood if you cover their latest crap-fest in icing and candy sprinkles. I even read aintitcool sometimes, but not for serious critique.
I've met Harry, and I think he genuinely does like a lot of movies, I don't think he's selling out to corporations or anything, he was doing this long before he got noticed. To put it kindly, though, his tastes are a bit, shall we say, broad?
There may be a theater in your area that doesn't show ads. Look here for more information.
Here in Austin, there's a place called the Alamo Drafthouse, and not only are they commercial free, they show cool/funny clips from old movies for the early birds. I refuse to watch movies anywhere else.
No, they can't. Sorry about that.
Just in the general case (disregarding this "proof"), what if a million dollar "proof" is published, is found to have a non-trivial error, and someone else fixes the error and publishes a very similar proof largely based on the incorrect one, which turns out to be correct? Who gets the money? Who decides the triviality of the error?
It is a virus used by terrorists. It stands for "Internet Exploder".
That is just too funny. Any bets how long it takes them to fix it? I keep getting this picture in my mind of the Seinfeld with Kramer and his little person friend, wearing the same shirt, and the little person says "We'll look like idiots!"
I haven't. For some reason the page won't load. Why is that?
Yes, it will protect your lap, but what about your laptop? It may not be designed to absorb all of that heat.
Just because you have fun with your python doesn't mean you have to tell us all about it. Did you also have a minor kernal release?
That's 11,500 years ago. We have only recently discovered that the earth's temperature rose anywhere from 3-12 degrees in less than a decade almost exactly 11,500 years ago. Even if you say that it's not meaningful, it is a strong coincidence. Look up the word in the dictionary.
There's an old theory in paleoclimatology called "steady state theory" which holds that changes in the earth's climate happen very gradually, over hundreds of years. We know now beyond any reasonable doubt that that theory is false when it comes to temperature changes. Glacier core samples do not lie. Scientists have come up with all kinds of explanations for why nearly every culture has a global flood myth, including your river speculation, except for the obvious: there was a flood.
Paleoclimatologists and geologists are starting to come around to my way of thinking.
We've found several submerged cities in the past, right around the various coastlines. People like to live right on the coast. If the ocean level rose rapidly, it would kill off most of the earth's human population, and it would appear the whole earth had flooded (and it would appear a large island like Atlantis had sunk).
That's just speculation on your part. It is a pretty interesting coincidence that almost exactly the time that Plato said Atlantis sank, we know the temperature of the Earth raised drastically. I can provide hard science to back this up if you don't believe me.
We also know that the ocean level was 300-500 feet lower before then. There is ongoing debate about how fast the ice melted and how fast the ocean level rose, but if it rose even 50 feet rapidly, a very large island (as Atlantis is purported to have been) would appear to sink almost overnight.
If it's an allegory, those facts make for a hell of a coincidence.