What's the Cost of a Try Block

Discussion in 'C++' started by Tiglath, Jan 19, 2007.

  1. Tiglath

    Tiglath Guest

    We are building a high performance system and suddenly the cost of
    using exception has been magnified.

    What is the actual cost of having a frequent call inside a try-catch
    block when the vast majority of times there will be no exception to
    catch?

    Many thanks.
    Tiglath, Jan 19, 2007
    #1
    1. Advertising

  2. Tiglath

    Noah Roberts Guest

    Re: What's the Cost of a Try Block

    Tiglath wrote:
    > We are building a high performance system and suddenly the cost of
    > using exception has been magnified.
    >
    > What is the actual cost of having a frequent call inside a try-catch
    > block when the vast majority of times there will be no exception to
    > catch?


    I would think that someone experienced in building high performance
    systems would know how to use a profiler...go figure.

    It's implementation dependent. Could be nothing...could be an increase
    in size....could be an increase in time...

    You'll just have to use the scientific method.
    Noah Roberts, Jan 19, 2007
    #2
    1. Advertising

  3. Tiglath

    bjeremy Guest

    Re: What's the Cost of a Try Block

    Tiglath wrote:
    > We are building a high performance system and suddenly the cost of
    > using exception has been magnified.
    >
    > What is the actual cost of having a frequent call inside a try-catch
    > block when the vast majority of times there will be no exception to
    > catch?
    >
    > Many thanks.


    Scott Meyers, "More Effective C++" has a good analysis on this, you
    should read it.. but the gist is there will be an overhead associated
    with exceptions, however if you do not incurr exceptions often (which
    should be the case!), you probably will only take about a 5% hit in
    performance... He cites studies in his book, I don't have it handy for
    reference, sorry. But, thumb through it, its a pretty good book.
    bjeremy, Jan 19, 2007
    #3
  4. Tiglath

    Pete Becker Guest

    Re: What's the Cost of a Try Block

    bjeremy wrote:
    >
    > Scott Meyers, "More Effective C++" has a good analysis on this, you
    > should read it.. but the gist is there will be an overhead associated
    > with exceptions, however if you do not incurr exceptions often (which
    > should be the case!), you probably will only take about a 5% hit in
    > performance... He cites studies in his book, I don't have it handy for
    > reference, sorry. But, thumb through it, its a pretty good book.
    >


    Be sure to check out which compilers he's talking about. Compilers for
    Windows used to use SEH for exceptions, and that, indeed, added runtime
    overhead. Newer compilers are getting away from that, since it's not
    really appropriate. And compilers for other platforms never did it.
    Hypothetically, exceptions can be implemented with no runtime overhead
    if you don't throw exceptions (except possibly for lost optimization
    opportunities), and some compilers do just that.

    As ever, the answer is, measure it.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Jan 19, 2007
    #4
  5. Tiglath

    Grizlyk Guest

    Re: What's the Cost of a Try Block

    Tiglath wrote:

    > We are building a high performance system and suddenly the cost of
    > using exception has been magnified.
    >
    > What is the actual cost of having a frequent call inside a try-catch
    > block when the vast majority of times there will be no exception to
    > catch?


    If you do not use exceptions as return value for one of correct state,
    you will have no overhead if no error condition will occur.

    This is one of example:

    //code without exceptions
    movl %esp, %ebp
    pushl %ecx
    subl $20, %esp
    call _test
    leave
    ret

    //the same code with exceptions
    movl %esp, %ebp
    pushl %ecx
    subl $20, %esp
    call _test
    jmp L28
    //catch
    call ___cxa_begin_catch
    call ___cxa_end_catch
    L28:
    leave
    ret

    if no error condition will occur, program with exception will be longer
    by one "jump" opcode. Of course, size of program with exception will be
    greater, than without, but you can not make size smaller even with
    manual control:

    if(error_condition){print_error(); return;}
    do_any();

    will always ne longer then just

    do_any();

    Compiler even can tell CPU what kind of jump condition has more
    priority for anticipatory memory access, if CPU supports it.
    Grizlyk, Jan 20, 2007
    #5
  6. Tiglath

    peter koch Guest

    Re: What's the Cost of a Try Block

    Tiglath skrev:
    > We are building a high performance system and suddenly the cost of
    > using exception has been magnified.


    How? Did you measure the performance or is it just something you
    believe is so?

    >
    > What is the actual cost of having a frequent call inside a try-catch
    > block when the vast majority of times there will be no exception to
    > catch?


    I don't believe there will be any overhead at all, but I'd question
    your design anyway. Why do you need a try-catch around a function that
    you believe is a bottleneck? In my experience, proper C++ code has very
    few try-catch blocks, and they are never at such low a level.

    /Peter
    peter koch, Jan 20, 2007
    #6
    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. bienwell
    Replies:
    4
    Views:
    3,713
    bienwell
    May 27, 2005
  2. Steven T. Hatton

    What does it cost to try?

    Steven T. Hatton, Aug 6, 2004, in forum: C++
    Replies:
    2
    Views:
    284
    David Hilsee
    Aug 6, 2004
  3. John Salerno
    Replies:
    20
    Views:
    820
    John Salerno
    Aug 11, 2006
  4. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  5. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    361
Loading...

Share This Page