I mean in the current implementation of clang or the gcc version.
C++ and Java guys always tell me that exceptions do not cost any performance unless they are thrown. Is the same true for Objective-C?
Exception handling is made available in Objective-C with foundation class NSException. @try − This block tries to execute a set of statements. @catch − This block tries to catch the exception in try block. @finally − This block contains set of statements that always execute.
Objective-C uses the runtime code compilation Generally, this happens very fast but when the code compilation happens a significant number of times, it becomes measurable. Objective-C is a superset of C and all C functions that you will write in Objective-C will be just as fast.
C++ and Java guys always tell me that exceptions do not cost any performance unless they are thrown. Is the same true for Objective-C?
Short Answer
Only in 64-bit OS X and iOS.
They're not entirely free. To be more precise, the model is optimized to minimize costs during regular execution (moving consequences elsewhere).
Detailed Answer
On 32 bit OS X and iOS, exceptions have runtime costs even if they are not thrown. These architectures do not use Zero Cost Exceptions.
In 64 bit OS X, ObjC moved over to borrow C++'s "Zero Cost Exceptions". Zero Cost Exceptions have very very low execution overhead, unless thrown. Zero Cost Exceptions effectively move execution cost to binary size. This was one of the primary reasons they were not initially used in iOS. Enabling C++ exceptions and RTTI can increase the binary size by more than 50% -- of course, I would expect those numbers to be far lower in pure ObjC simply because there is less to execute when unwinding.
In arm64, the exception model was changed from Set Jump Long Jump to Itanium-derived Zero Cost Exceptions (judging by the assembly).
However, Idiomatic ObjC programs are not written or prepared to recover from exceptions, so you should reserve their use for situations which you do not intend to recover from (if you decide to use them at all). More details in the Clang manual on ARC, and in other sectons of the referenced page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With