Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2005-06-23 03:36:36


Tan Kwee Heong wrote:

> The costs of throwing an exception is well described in Scott Meyer's Item
> 15 "More Effective C++". The key guidelines
> presented were :
> a) ... exceptions should be rare. After all, they indicate the
> occurrence of events that are exceptional.
> b) Compared to a normal function return, returning from a
> function
> by throwing an exception may be as much as three orders of magnitude
> slower.

So, instead of a single return instruction, your processor will execute
1000? Or something else is meant? Oh, well, armed with such tool as
valgrind, I can check it myself. See the example at:
http://zigzag.cs.msu.su/~ghost/exception_handling/single_throw
I've used g++ 3.3 with -O3

With return: 1,849,814 instructions
With exception: 1,956,753 instructions

Quite a difference. Ok, but this might include reading the exception tables
the first time the exception is thrown.

Let's try calling the function in a loop:
http://zigzag.cs.msu.su/~ghost/exception_handling/multiple_throw

10 calls to function:
With return 1,848,380
With exception 2,134,423

11 calls to function:
With return: 1,848,389 (+9 instructions)
With exception: 2,154,161 (+19738)

Ratio is 2193.

Now, let's create a graph of 10 vertices and call depth_first_search on it:
http://zigzag.cs.msu.su/~ghost/exception_handling/multiple_with_some_work:

10 calls
With return: 2,077,392
With exception: 2,267,514

11 calls
With return: 2,085,819 (+8427)
With exception: 2,309,015 (+41501)

Ratio: 5

Hmm.... I suspect that for a realistically sized graph the speed differences
will become insignificant. And for realistically sized graph data cache
missed might come into play, further alleviating the difference.

So, this boils down two:
1. Yea, in completely refined example exception is a three orders of
magniture slower.

2. In practice, you need to use profiler, because that difference might
evaporate.

- Volodya


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net