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;
}
you could try fast enumeration:
this should avoid extra retains/releases, as in fast enumeration the whole array is blocked.