James Kanze
9/6/2009 8:54:00 PM
On Sep 6, 4:12 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Christof Donat wrote:
> > Java needs to be interpreted or JIT compiled, while C++ is
> > compiled directly into machine code. At compile time the
> > compiler with knowledge of the CPU can heavily optimize the
> > code which is not possible in a JIT, that has much stricter
> > run time requirements.
> One factor which makes C++ faster is that it's an "unsafe"
> language. This means that clock cycles are not spent on
> boundary checks and other such checks which, in a bug-free
> program, are 100% useless overhead.
That's not true. C++ makes many things "undefined behavior",
where Java requires a specific behavior (usually the wrong
behavior), but "undefined behavior" means that an implementation
is free to do whatever is best for its users, and all of the
implementations I regularly use (now that I'm no longer using
Sun CC) do implement bounds checking, at least for the standard
containers.
> Of course many people don't like to program in an "unsafe"
> language, so your mileage may vary.
> C++ is not faster than Java in everything. Memory allocation
> in most systems is considerably slower in C++ (and C) than in
> java. (OTOH Java lives on constant memory allocation due to
> how the language works, while most C++ programs don't.)
> > It depends on what you compare.
> > There are shootouts that compare C++ code compiled for a 386
> > with 387 FPU with Suns Hot Spot engine on a current CPU.
> > Since the java engine knows the CPU that it is running on,
> > it can optimize for the MMX and SSE units and the more
> > subtle issues of that specific CPU. Those shootouts are
> > usually used to "prove" that java was faster than C++.
> You don't even have to resort to such dirty trickery to
> "prove" it. Just make a program which allocates 10 million
> small objects dynamically in both languages, and there you go:
> You have proven Java to be vastly faster than C++.
More realistically, tell me which language you want to win, and
I'll write a benchmark which "proves" it to be faster. For
Java, lots of array accesses with arrays of basic types, with
the arrays passed to functions for the actual work; for C++,
just change the type of the array member to a very simple struct
(which means that every element must be individually allocated
in Java), with many short lived (but large) arrays.
--
James Kanze