6

Would this cause a performance penalty, compared to calling blabla without the try block?

-(void)bla{
    @try{
        [self blabla];
    }
    @catch (NSException *e) {
        // Do nothing
    }
}
Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • 2
    I've seen this asked here for C and C++. Although I'm not positive the answer is the same, I'm betting it is. Anyway, you ought to check out the "compared to what" threads in the answers for both of those languages. For example, here's one: http://stackoverflow.com/questions/16784601/does-try-catch-block-decrease-performance – Turix Feb 25 '14 at 20:35
  • There is possibly some minor loss of compiler optimization, but probably nothing else. (And "Objective-C compiler optimization" is something of an oxymoron anyway.) – Hot Licks Feb 25 '14 at 20:38
  • 1
    Not much of a problem since we don't throw exceptions that often in Objective-C :) – Gavin Feb 25 '14 at 20:44
  • 1
    @Gavin ... and we can't catch them reliably. – Nikolai Ruhe Feb 25 '14 at 20:47

3 Answers3

5

from doc

Zero-Cost @try Blocks

64-bit processes that enter a zero-cost @try block incur no performance penalty. This is unlike the mechanism for 32-bit processes, which calls setjmp() and performs additional “bookkeeping”. However, throwing an exception is much more expensive in 64-bit executables. For best performance in 64-bit, you should throw exceptions only when absolutely necessary.

so no overhead for 64-bit processes

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • 1
    It's important to notice that this is true for Mac OS X only. On iOS the implementation always was like the 64 Bit Mac architecture. – Nikolai Ruhe Feb 25 '14 at 20:45
4

You might be interested in this blog here: LLVM PROJECT BLOG

That is, on Intel and since Okt. 2013, on ARM too, C++ exceptions are now "zero-cost".

The Objective-C exceptions are realized in terms of this implementation.

However, the need for an unwinder will disable a few optimization opportunities, so that code which requires to handle exceptions might be less optimal optimized that when no exceptions have to be handled.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
4

On some platforms, @try/@catch is "zero cost". There is no additional code executed for a try block as long as no exception is thrown. There is extra overhead in the form of exception unwinding data, but that also occupies no real memory until an exception is thrown. These platforms include 64-bit OS X and 64-bit iOS.

On some platforms, merely entering @try incurs some CPU overhead to save registers. These platforms include 32-bit OS X and 32-bit iOS.

Greg Parker
  • 7,972
  • 2
  • 24
  • 21