What's the Cost of a Try Block

T

Tiglath

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.
 
N

Noah Roberts

Tiglath said:
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.
 
B

bjeremy

Tiglath said:
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.
 
P

Pete Becker

bjeremy said:
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)
 
G

Grizlyk

Tiglath said:
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.
 
P

peter koch

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top