Re: Java outperforms C++?
- From: "Bo Persson" <bop@xxxxxx>
- Date: Fri, 15 Apr 2005 19:36:27 +0200
|
"$Scott" <$Scott@xxxxxxxxxxxxxxxxxx> skrev i meddelandet news:d3n7t1$rfq$2$8300dec7@xxxxxxxxxxxxxxxxxxx > > "Bo Persson" <bop@xxxxxx> wrote in message > news:eJcqHiRQFHA.1096@xxxxxxxxxxxxxxxxxxxxxxx >> >> >> His examples shows that it is *possible* to write C++ code that runs >> slower than some Java code. > > It is possible to write slow code - what a trivial observation! It has > absolutely no relevance to which language is faster. It does, if you use the code in a benchmark to try to decide exactly that! The code shown isn't even C++ code, but lousy C code. It shows that it is *possible* to write slow code, but not that code I write would run that slow. > Nor does it appear to > have any connection to any benchmark in Lewis and Neumann's paper. Nor does > it invalidate their reasons for why Java turns out to be faster. But the code is from one of the benchmarks quoted. > >>How does that prove that Java is faster? > > Go back and read what I said a bit more carefully, then read Lewis and > Neumann's paper and understand why pointer optimization is so difficult in > C++ compared to Java array bounds checking, why garbage collection has > better cache performance than new/malloc and why run-time compilation offers > optimizations which a conventional "pre-compiler" cannot do. These are the > reasons why Java often outperforms C++. I *have* read their paper, but obviously come to a totally different conclusion. Let's take the test one at a tine: 1. Numerical kernels " The authors test some real numerical codes (FFT, Matrix factorization, SOR, fluid solver, N-body) on several architectures and compilers. On Intel they found that the Java performance was very reasonable compared to C (e.g, 20% slower), and that Java was faster than at least one C compiler (KAI compiler on Linux). " Here it says that Java is 20% slower than C, not faster. Right? The only C compiler that performed worse was the KAI compiler, that has since gone out of business. Wonder why! 2. More numerical methods. Here IBM Java performs worse than gcc 2.9x, which has never been considered a speed monster. GCC supports a number of languages on a very large number of platforms. Very portable, and very cheap, but it has never been accused of being highly optimizing. So, IBM Java is just slightly slower than a not-so-good C compiler. 3. Still more numerical methods Total time for three unknown tests - Unknown C compiler 6.9 seconds, Unkown Java 10.8 seconds. Who wins this? Where is the C++ version? 4. Microbenchmarks This contains the Game of life, which isn't coded in proper C++, but in bad C. 5. Microbenchmarks (again) Here we have some Java beating up C compilers on advanced code like: int intResult = 1; int i = 1; while (i < intMax) { intResult -= i++; intResult += i++; intResult *= i++; intResult /= i++; } Whenever I write my C++ code like this, I want to be taken out and shot! Nevertheless, the numbers for Visual C++ shows that it runs these tests twice as fast as the Java versions. You could also note that it easily beats gcc, the reference used in test 2. Now, lets look at the theory. 1) Pointer aliasing is a problem in C, where int* and char* might be
common. A char* is expecially nasty, because it is allowed to point to any byte
in the program.
In C++, pointers are much less used, which minimizes this problem. On top
of that, a pointer to a class can not alias a pointer to any other class or data
type. C++ optimizers use this knowledge!
In the example arr[j] is allowed to alias the int variable x.
In C++ a std::vector<int> can never, ever alias any other int
variables. Do you know what this means? C++ is faster than C !
2) Garbage collection can be faster than manual memory allocation.
Agreed! :-)
Especially for a benchmark, where you allocate and release lots and lots of
small elements. If you have enough memory, you will never reach the point where
the collection starts.
I have seen this used to "prove" that C#, too, is faster than C++.
for (int i = 0; i != 10000000; ++i)
{
int* p = new int;
delete p;
}
is definitely slower than
for (int i = 0; i != 10000000; ++i)
{
int* p = new int;
} 3) Run-time compilation
Not true at all!
If I care about the speed of the program, I compile it for a P4 or an AMD64
target. Who cares how fast it runs on a PIII??
Now for a surprise, the compiler and linker pair can also figure out which
code is used in a program!
Branch prediction is done in hardware. If you really care, you run a test
and feed the profile back into the compiler.
Now for another surprise, you cannot always trust Microsoft
marketing!
Ok, so where did the article show that Java is faster than C++?
I found their conclusion to be:
" Java is now nearly equal to (or faster than) C++ on low-level and numeric
benchmarks. "
So, sometimes Java can be almost as fast as C++.
Especially if it isn't actually C++, but really bad C code, and using an average
or worse compiler.
Bo Persson
|
- Follow-Ups:
- Re: Java outperforms C++?
- From: Alex
- Re: Java outperforms C++?
- References:
- Java outperforms C++?
- From: benben
- Re: Java outperforms C++?
- From: $Scott
- Re: Java outperforms C++?
- From: Jerry Coffin
- Re: Java outperforms C++?
- From: $Scott
- Re: Java outperforms C++?
- From: Bo Persson
- Re: Java outperforms C++?
- From: $Scott
- Java outperforms C++?
- Prev by Date: Re: Java outperforms C++?
- Next by Date: Re: Java outperforms C++?
- Previous by thread: Re: Java outperforms C++?
- Next by thread: Re: Java outperforms C++?
- Index(es):
Relevant Pages
|