Did the effect of self.instance_var hit?

Let mFoo be the instance variable that has been synthesized, so it has a default installer and getter. I am wondering if you need to worry about the efficiency of using self.mFoo vs. mFoo if mFoo repeatedly accesses logical operators.

It seems to me that if someone is absolutely sure that the method does not have a local variable mFoo, and one makes several logical comparisons with mFoo inside the method, and the method is called a lot, it makes sense not to access mFoo through its access method, but directly .

For example:

 NSMutableArray *mFoo; @property(nonatomic, retain) NSMutableArray *mFoo; @synthesize mFoo; -(void)someMethod { Bar* b; for (int i=0; i<10000; i++) { b = [self.mFoo objectAtIndex:i]; // <<<<<<<<<<<<<<<< if (b.something == 123) { // do something }; } } 
0
objective-c
Feb 20 '10 at 16:50
source share
3 answers

It seems to me that if someone is absolutely sure that the method does not have a local variable mFoo declared, and one makes a somewhat logical comparison with mFoo within the method, and this method is called a lot, it makes sense not to access mFoo through its access method, but directly.

Always use accessors. Always . Never capture values ​​directly when viewing an object as a structure. This violates encapsulation, etc. Etc. Etc....

(Keep in mind that self.mfoo is exactly the synonym for [self mfoo] in the next discussion).

Now in this case, your obvious optimization is to move self.mfoo outside the loop. Use the syntax for(Bar *b in self.mfoo) as suggested by Mr. Mag.

However, the real question is: do you measure the performance of your application and determine what the call that actually caused significant overhead?

If so, optimize. Most likely, although there are no measurable overheads in the grand scheme of things.

+4
Feb 20 '10 at 18:09
source share

If you access a property from another class, you should always use the access method or dot notation. This is just basic OO hygiene. In the unlikely event that you find that this causes a bottleneck in the loop or something like that, you might think of some sort of individual solution at this point.

If you are accessing a variable from a class, I think this is normal if you access ivar directly, and not through a property. However, if you set a value, you should always use the property, both to avoid memory leaks, and because the installer may have logic that must be executed.

So:

 CGSize sz = [self size]; //ok CGSize sz = self.size; //ok CGSize sz = _size; //ok, but be careful [self setSize:sz]; //ok self.size = sz, //ok _size = sz; // DON'T EVER DO THIS! 

However, if you have ivar, which does not even have a property associated with it, you, of course, will need to access it directly (you have little choice ...). Personally, I usually declare properties for all ivars, except for some flags or counters with non-object types, but many people do not.

+3
Feb 20 '10 at 17:02
source share

For each access to self.mFoo it is necessary to call one C function if fast sending of the Objective-C message is enabled (if the compiler does not optimize this call). Thus, a performance hit will not be noticeable most of the time. In the above example, you should use a quick enumeration like

 for (Bar *b in self.mFoo) { // do whatever you wish to here } 

which will lead to access to self.mFoo only once and faster overall (for example, if your array is really a list).

0
Feb 20 '10 at 16:55
source share



All Articles