Reputation: 1207
I'm using ARC in my iOS app. I have profiled the method below and even though the algorithm is terribly naive and wasteful 77% of the time is spent in objc_retain
and objc_release
. I think it must be down to the line where i fetch a Unit from the NSArray
and ARC is careful to retain and then release the object each time.
I'm looking for an informed advice: how do I fix it elegantly?
-(CGFloat)getUncertaintyForUnits:(NSArray*)units Position:(MKMapPoint)position Zoom:(MKZoomScale)zoomScale {
CGFloat closest = MAXFLOAT;
for (int i = 0; i < [units count]; i++) {
Unit *units = (Unit*)[units objectAtIndex:i];
CGFloat distance = [self distanceBetweenMapPoints:unit.mapPoint And:position];
if (distance < closest) {
closest = distance;
}
}
CGFloat max = 100 / zoomScale;
return (1. - closest / max) * 0.9;
}
Upvotes: 4
Views: 165
Reputation: 70673
Unless you need memory managed objects for some reason, one faster alternative would be to store your position points in regular C arrays (or an array of structs).
Objective C is a superset of regular ANSI C. Sometimes the benefits of the OOP superset constructs do no outweigh the costs in object runtime messaging and management in inner loops.
Upvotes: 0
Reputation: 52227
you could try fast enumeration:
for (Unit *unit in units) {
CGFloat distance = [self distanceBetweenMapPoints:unit.mapPoint And:position];
if (distance < closest) {
closest = distance;
}
}
this should avoid extra retains/releases, as in fast enumeration the whole array is blocked.
There are several advantages to using fast enumeration:
- The enumeration is considerably more efficient than, for example, using NSEnumerator directly.
- The syntax is concise.
- Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised.
Upvotes: 10