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:
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:

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:

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

- Volodya

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at