By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,678 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,678 IT Pros & Developers. It's quick & easy.

What's the Cost of a Try Block

P: n/a

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.

Jan 19 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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.

Jan 19 '07 #2

P: n/a

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.

Jan 19 '07 #3

P: n/a
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)
Jan 19 '07 #4

P: n/a
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.

Jan 20 '07 #5

P: n/a

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

Jan 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.