Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C properties - is using [self myProperty] or self.myProperty slower than myProperty?

I'm using Objective-C properties to handle retaining/releasing instance variables for me. In my class, i'm doing stuff like this:

self.myProperty = somethingIWantToRetain
[self.myProperty doSomeAction]
[self.myProperty doSomethingElse]

Is using [self myProperty] / self.myProperty slower than simply using myProperty for those lines where i'm not changing the value of myProperty? Eg would the following be faster?

self.myProperty = somethingIWantToRetain
[myProperty doSomeAction]
[myProperty doSomethingElse]

Thanks

like image 968
Chris Avatar asked Dec 09 '22 07:12

Chris


1 Answers

It's almost certainly a little bit slower, but it's unlikely to matter much.

Referring to your ivar directly (with a naked myProperty) accesses the variable directly. Referring to your property getter (with the equivalent self.myProperty or [self myProperty]) has to invoke a method, which will generally perform a retain and autorelease on your ivar.

However, method dispatch in Objective-C is very, very fast, and the retain/autorelease calls are pretty cheap as well, especially for objects that will likely not be destroyed when the autorelease pool is cleared. I would focus on readability and consistent style, and only worry about performance here when it becomes clear that you have performance bottlenecks to chase.

like image 87
Seamus Campbell Avatar answered Jan 01 '23 03:01

Seamus Campbell