On C/C++ undefined behaviours

dsimcha dsimcha at yahoo.com
Fri Aug 20 10:59:10 PDT 2010


== Quote from Jonathan M Davis (jmdavisprog at gmail.com)'s article
> On Friday, August 20, 2010 10:04:41 Andrej Mitrovic wrote:
> > What are these Java programs for the desktop that run fast? I haven't
> > encountered any, but maybe that's just because I didn't try them all
> > out. Eclipse takes at least 20 seconds to load on startup on my quad
> > core, that's not very fast. On the other hand, CodeBlocks which is
> > coded in C++ and has  a few dozen plugins installed runs in an
> > instant.
> There's plenty of Java code which runs just as fast or faster than comperable
> C++ code. I've seen it. However, you're generally talking about small,
> computation-intensive programs. When you're talking about full-blown desktop
> applications, there's so much more going on than math operations that the game
> is entirely different. I'm not sure that there are any full-blown desktop Java
> applications which are all that efficient in comparison to similar C++
> applications. They're definitely useable, but not necessarily as efficient. Of
> course, the difference between Ecilpse and CodeBlocks could be entirely a design
> issue rather than a language one. Once you're dealing with anything that
> complex, accurately comparing apps can be difficult. It could just as easily be
> that Eclipse's design isn't as efficient  - be it due to features that it has
> which CodeBlocks doesn't or poor design, or whatever.
> - Jonathan M Davis

The thing about Java is that, even if equivalently written Java code is as fast as
C++ code, if you really care about performance you're gonna use dirty tricks in a
few key hotspots.  Java makes dirty tricks hard or impossible to use.  For
example, Java's stock GC is very good, but when you're trying to squeeze out that
last drop of performance you might want to use some kind of region- or stack-based
memory management.  This is unimplementable in Java.  You also can't use tricks
like recycling the same buffer for multiple types.

Another area where dirty tricks are sometimes useful is type punning to "cheat"
and speed up algorithms.  I actually sped my sorting functions in dstats up by
~20% on floating point numbers by performing a single pass over the array to pun
the numbers to integers and bit twiddle them such that their ordering is
unaffected by this punning, sorting them as integers, and then untwiddling and
casting them back.  Floating point comparisons have to watch out for things like
NaNs and are thus slower than integer comparisons.  These sorting routines, using
DMD's crappy optimizer, are now faster than STL's sorting routines, using GCC's
optimizer, for sorting floating point numbers.

As another example, I'm pretty sure the fast inverse square root algorithm
(http://en.wikipedia.org/wiki/Fast_inverse_square_root) is unimplementable in Java.


More information about the Digitalmars-d mailing list