How do C ++ exceptions slow down code if there are no exceptions?

I read that there is some overhead to using C ++ exceptions to handle exceptions, as opposed to, say, checking return values. I am only talking about the overhead that occurs when an exception occurs. I also assume that you will need to implement code that actually checks the return value and does the appropriate thing, regardless of what would be equivalent to what the catch block would do. And it is also unfair to compare code that raises exception objects with 45 internal variables inside code that returns a negative integer for each error.

I am not trying to create a case for or against C ++ exceptions based solely on what can be done faster. I heard that someone recently made the case that code using exceptions should run as fast as code based on return codes as soon as you take into account all the additional accounting code you need to check the return values ​​and handle errors. What am I missing?

+45
c ++ exception overhead
Dec 13 '09 at 22:03
source share
6 answers

There is a cost associated with handling exceptions on some platforms and with some compilers.

Namely, when creating a 32-bit target, Visual Studio will register a handler in each function with local variables with a nontrivial destructor. Basically, it installs a try/finally handler.

Another method used by gcc and Visual Studio targeting 64-bit bits carries only overhead when throwing an exception (the method involves moving the call stack and searching the table). In cases where exceptions are rarely thrown, this can lead to more efficient code, since error codes should not be handled.

+41
Dec 13 '09 at 10:15
source share

Just try / catch and try / save the block, follow a few setup instructions. Overhead costs should usually be negligible in each case, with the exception of the most complex cycles. But you will not use try / catch / except in the inner loop at all.

I would advise you not to worry about this and use a profiler instead to optimize your code where necessary.

+11
Dec 13 '09 at 22:07
source share

It is entirely implementation dependent, but many recent implementations have very little or no performance overhead when exceptions are thrown. You are actually right. Correctly checking return codes from all functions in code that does not use exceptions can be slower than doing nothing for the code using exceptions.

Of course, you will need to accurately measure performance for your specific requirements.

+7
Dec 13 '09 at 22:08
source share

There are some overhead with exceptions (as other answers pointed out).

But now you have no choice. Try disabling exceptions in your project and make sure ALL-dependent code and libraries can compile and work without it.

Do they work with disabled exceptions?

Suppose they do! Then check out some cases, but note that you need to install the "disable exceptions" compiler. Without this switch, you still have overhead β€” even if the code never throws exceptions.

0
Dec 13 '09 at 22:27
source share

Only the overhead is ~ 6 instructions, which add 2 SEH at the beginning of the function and leave them at the end. No matter how many attempts / catches you have in the stream, it is always the same.

And what about local variables? I hear people always complain about them when using try / catch. I do not understand this, because in the end the deconstructors will be called. Also, you should not allow the exception to go up more than 1-3 calls.

-one
Dec 13 '09 at 22:56
source share

I took the Chip Uni test code and expanded it a bit. I split the code into two source files (one with exceptions, one without). I did every performance test 1000 times, and I used clock_gettime() with CLOCK_REALTIME to record the start and end times of each iteration. Then I calculated the average and variance of the data. I tested this test with 64-bit versions of g ++ 5.2.0 and clang ++ 3.7.0 on an Intel Core i7 core with 16 gigabyte RAM that runs ArchLinux with a 4.2.5-1-ARCH kernel. You can find the extended code and full results here .

g ++

With no exceptions
  • Average: 30,022,994 nanoseconds
  • Standard Deviation: 1.25327e + 06 nanoseconds
Exceptions
  • Average: 30,025,642 nanoseconds
  • Standard Deviation: 1.83422e + 06 nanoseconds

clank ++

With no exceptions
  • Average: 20 954 657 nanoseconds.
  • Standard Deviation: 426,662 nanoseconds
Exceptions
  • Average: 23 916 638 nanoseconds.
  • Standard Deviation: 1.72583e + 06 nanoseconds

C ++ exceptions only allow non-trivial performance limits with clang ++, and even this penalty is only ~ 14%.

-2
Nov 29 '15 at 18:55
source share



All Articles