Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What's the Cost of a Try Block (http://www.velocityreviews.com/forums/t459936-whats-the-cost-of-a-try-block.html)

Tiglath 01-19-2007 05:09 PM

What's the Cost of a Try Block
 

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.


Noah Roberts 01-19-2007 05:23 PM

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.


bjeremy 01-19-2007 06:10 PM

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.


Pete Becker 01-19-2007 06:17 PM

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)

Grizlyk 01-20-2007 05:18 AM

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.


peter koch 01-20-2007 02:35 PM

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



All times are GMT. The time now is 12:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.