Performance and Exceptions

Discussion in 'Java' started by Chris, Aug 9, 2006.

  1. Chris

    Chris Guest

    Do methods that can potentially throw an Exception (but don't) execute
    any slower than methods that don't throw Exceptions?

    Do code blocks enclosed in try/catch execute slower that those that aren't?

    For example, does this execute slower:

    for (i = 0; i < 1000000; i++) {
    try {
    myObject.whatever();
    } catch (SomeUncheckedException e) {
    // handle it here
    }
    }

    than this?

    for (i = 0; i < 1000000; i++) {
    myObject.whatever();
    // ignore unchecked exception
    }
     
    Chris, Aug 9, 2006
    #1
    1. Advertising

  2. Chris

    EJP Guest

    Chris wrote:

    > Do methods that can potentially throw an Exception (but don't) execute
    > any slower than methods that don't throw Exceptions?
    >
    > Do code blocks enclosed in try/catch execute slower that those that aren't?


    Last time I looked into this, the main costs of exception handling occur
    when you throw & catch one. That was for fully-compiled code but offhand
    I don't see why it should be too different for an interpretive system.
     
    EJP, Aug 9, 2006
    #2
    1. Advertising

  3. Chris

    jmcgill Guest

    EJP wrote:
    > Chris wrote:
    >
    >> Do methods that can potentially throw an Exception (but don't) execute
    >> any slower than methods that don't throw Exceptions?
    >>
    >> Do code blocks enclosed in try/catch execute slower that those that
    >> aren't?

    >
    > Last time I looked into this, the main costs of exception handling occur
    > when you throw & catch one. That was for fully-compiled code but offhand
    > I don't see why it should be too different for an interpretive system.


    An O'Reilly benchmark, in the Java Performance Tuning book,
    tests the extra cost of putting a try-catch with no exception, in a
    loop, instead of around the loop.

    This is not a compiler test, it's a vm test. It turns out to be pretty
    hard to set up, since any decent (recent) compiler will do a good job of
    optimizing your test away.

    Most of the VM's tested had a 10% increase in time for having the
    try-catch inside the loop, especially those running a JIT.

    A much more interesting test in the same chapter, shows that a
    conditional instanceof is far more efficient than trying and catching a
    ClassCastException.
     
    jmcgill, Aug 9, 2006
    #3
  4. Chris

    Chris Uppal Guest

    Chris wrote:

    > Do methods that can potentially throw an Exception (but don't) execute
    > any slower than methods that don't throw Exceptions?


    To a first approximation, no. "setting up" an exception handler is (or
    damn-well should be) a zero-cost operation.

    However the possibility that an exception might be thrown and caught may
    interfere with optimisations performed by the JITer, so it is /possible/ that a
    loop without exception handling could run faster than one with.

    -- chris
     
    Chris Uppal, Aug 9, 2006
    #4
  5. Chris

    Chris Smith Guest

    Chris Uppal <-THIS.org> wrote:
    > To a first approximation, no. "setting up" an exception handler is (or
    > damn-well should be) a zero-cost operation.


    Just to be picky, that's only true if you assume no optimization.
    Exception handling does limit the possibilities for code optimizers, by
    essentially inserting a lot of control flow possibilities that can
    prevent the compiler from reordering certain statements in order to fill
    the CPU pipelines. In particular, if you catch something like
    NullPointerException that could happen at any time, then memory stores
    often can't be moved very much earlier, lest they get executed when they
    shouldn't and then a catch block observe the old value. I don't know
    exactly how big an issue this is, but at least my Muchnick book mentions
    it as being significant.

    --
    Chris Smith - Lead Software Developer / Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    30,122
    Chris Smith
    Jul 14, 2004
  2. Replies:
    28
    Views:
    7,453
    Matt Atterbury
    Jul 15, 2005
  3. Paul Miller
    Replies:
    3
    Views:
    1,063
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    634
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    707
Loading...

Share This Page