gpt4 book ai didi

objective-c - 使用 ARC 保留/释放正在减慢我的速度

转载 作者:太空狗 更新时间:2023-10-30 03:58:59 26 4
gpt4 key购买 nike

我在我的 iOS 应用程序中使用 ARC。我已经分析了下面的方法,尽管该算法非常幼稚和浪费,但 77% 的时间花在了 objc_retainobjc_release 上。我认为这一定是我从 NSArray 中获取一个 Unit 并且 ARC 每次都小心地保留然后释放该对象的那一行。

我正在寻找明智的建议:如何优雅地修复它?

-(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;
}

最佳答案

你可以试试fast enumeration :

for (Unit *unit in units) {
CGFloat distance = [self distanceBetweenMapPoints:unit.mapPoint And:position];
if (distance < closest) {
closest = distance;
}
}

这应该避免额外的保留/释放,因为在快速枚举中整个数组都被阻塞了。

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.

关于objective-c - 使用 ARC 保留/释放正在减慢我的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com