Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the performance penalty of using isKindOfClass in Objective C?

I'm considering introducing:

if ([myInstance isKindOfClass:[SomeClass class]]) { do something...}

into a piece of code that gets called pretty often. Will I introduce a significant performance penalty? In Objective C, is there a quicker way of assessing whether a given object instance is of certain class type? For example, is the following quicker? (I realize the test is somewhat different)

if (myInstance.class == [SomeClass class]) { do something else...}
like image 223
durcicko Avatar asked Mar 28 '10 22:03

durcicko


1 Answers

The quickest way to see if two objects are of the same class is to compare their isa pointers. However, this means you'll miss when one object's class is a subclass of the other object's class, which is exactly what isKindOfClass: is meant for.

Regardless, the optimization you're talking about has already been done here:

http://www.mulle-kybernetik.com/artikel/Optimization/opti-3-imp-deluxe.html

like image 117
codewarrior Avatar answered Nov 19 '22 10:11

codewarrior