classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Classpath] style question


From: C. Scott Ananian
Subject: Re: [Classpath] style question
Date: Mon, 25 Feb 2002 01:55:05 -0500 (EST)

On Sun, 24 Feb 2002, Eric Blake wrote:

> Which is prefered, performing array bound checks throughout a method, or
> enclosing the algorithm in a try-catch block which converts an
> IndexOutOfBoundsException into a NumberFormatException?

For what it's worth: often adding explicit bounds checks will enable the
compiler to create faster code (by hoisting these checks, and others which
are implicit in the java semantics, out of the loop).  I'm not sure
which/any of the existing Java compilers are smart enough to do this, but
they all will be, eventually.

The try-catch block technique is sometimes faster for NullPointer
exceptions (because the exception handling is wrapped around the native
hardware memory protection mechanisms, and thus no hardware instructions
are required to do the checks in the common case), but I can't imagine any
case where the try-catch would be faster for array-bounds (okay, there are
some w/in *imagination*, but...).  In most cases, you'd be doing the same
array-bounds tests in hardware as in the explicit case, but then you'd
have to create the exception object and throw it, only to be caught,
rebuilt as another exception object, and rethrown.

Style needn't have anything to do w/ efficiency, though.  YMMV.
 --scott

Legion of Doom AK-47 DNC Rijndael Qaddafi payment Bush explosives 
Kennedy Indonesia biowarfare AES plastique Attache Saddam Hussein 
              ( http://lesser-magoo.lcs.mit.edu/~cananian )




reply via email to

[Prev in Thread] Current Thread [Next in Thread]