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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With