Re: How expensive are exceptions?
on Tue Jun 12 2007, "WalterHoward-AT-gmail.com" <WalterHoward-AT-gmail.com>
wrote:
You can argue theory all day. Why don't you just do some real world
testing.
Well, I have (long ago). The fact is that EH mechanisms can differ
quite a lot and the quality of integration with optimizers can differ
a lot too. But anyway, Sergey is (or at least he started out by)
making claims that certain overheads are unavoidable, which means the
argument _is_ about theory. I know that in at least some (maybe even
in all) real-world implementations, the EH implementation is not quite
optimal.
First of all, if you use any exceptions at all in the program, even
a single one, you have to compile the program with options that make
you pay the biggest price for using exceptions, setting up the stack
for every function to be exception aware. So even if you use just
one, you might as well use them everywhere because you already paid
the biggest price of all.
No such "setting up" is required in a range-table-based
implementation.
Second, I have done lots of performance testing on different
compilers and there is virtually no significant difference between
throwing an exception of type int, and returning an int and checking
the value.
That's really interesting. I'm guessing you've not tested too many
range-table-based implementations, because the ones I've looked at
make you pay quite a bit for even a little unwinding (in return for a
more efficient no-exception path).
To be fair when performance testing, you have to use the same type
"error" object, so if you like returning ints to check errors, you
have to throw int exceptions if you are doing performance
comparison. Also, remember, you have to do "if (return_code ==
failed)" type junk at every function call if you are using error
return values instead of exceptions (otherwise you aren't doing any
error handling which is an unfair test and bad programming). You
don't have to do that when throwing exceptions. You only need one
catch clause no matter how deep the call stack is.
Right.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]