-1

If we remove all exception-handling code of try-catch with error-return value, will there be constant performance increase or % performance increase in the code execution ?

Are there good articles/references that would explain cost of first throw,try-catch and cost of subsequent throw, try-catch blocks ?

  • The purpose of this question is not about finding threshold at which one might go for error-return style of coding over exceptions. We all know it is messy. (faster but messy).

  • I am looking to quantify cost of try-catch and impact of it w.r.t. 0 to 1 try-catch block and 1 to n try catch block.

Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
  • "if we remove all exception-handling code of try-catch with error-return value," - then you get a free ticket to code hell! – Mitch Wheat Feb 24 '13 at 07:48
  • Stop worrying about premature micro-optimisation. Provided you are not using try/catch for program control flow, it won't be a problem. Also, Benchmark first. – Mitch Wheat Feb 24 '13 at 07:49
  • There is no *hypothetically speaking* with regards to performance. There are only facts and figures achieved through profiling. – Alok Save Feb 24 '13 at 07:51
  • 1
    @OP Would it be fair to regard this as duplicate of http://stackoverflow.com/questions/1018800/performance-of-c0x-exceptions? – jogojapan Feb 24 '13 at 07:51
  • There is also this related question: http://stackoverflow.com/questions/43253/measuring-exception-handling-overhead-in-c – jogojapan Feb 24 '13 at 07:53

2 Answers2

1

This depends completely on the compiler and ABI -- there's no one answer.

It is possible for them to incur a small cost even when you don't throw. The setjmp/longjmp implementation, for example, is used by VC++. Generally this is an imperceptibly trivial cost, but it is there nonetheless.

It is also possible to have zero-cost exceptions -- that is, they are free until you throw. This is generally better for code which uses exceptions properly (keeps them rare and... exceptional).

Exceptions have potential to be even cheaper than your obvious C-style error handling. A smart compiler could recognize that catch blocks are going to be rare and relegate them into separate cache lines or pages, ensuring the "hot" non-exceptional code is kept as close together as possible.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • That's not really the whole story though. At least a few years ago merely enabling C++ exceptions in VC++ would incur a few percent of code bloat and a minor performance hit. This is mainly due to the compiler having to track local objects that need to be destroyed as the stack is unwound when throwing an exception. –  Feb 24 '13 at 08:47
0

The performance impact depends on the implementation of exception throwing and catching.

For example, using __try __except for SSH exceptions has a different overhead than "regular" try catch blocks.

Just benchmark it yourself on your system if you want to see for yourself.

The real "evil" in exceptions is the fact that they break the "normal" flow of the program.

This often causes programs to get into undefined states (depends on how well it's handled by the programmers).

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185