jjzjj

iOS 和 Objective-C : most of CPU time is spent in [NSObject release] and [NSObject retain] but class method is not doing any memory operations

coder 2023-09-21 原文

图像处理应用程序在模拟器上运行速度很快,但在真实设备 (iPhone 4GS) 上真的很慢

在“instruments”下运行应用程序时,我看到以下调用树:

请注意,据报告,红色圆圈内的调用几乎占用了该方法的所有 CPU 时间。

问题中的方法是类方法(不是实例方法),代码如下:

@implementation Line2F

+ (CGFloat)signTested:(Point2F *)tested p1:(Point2F *)p1 p2:(Point2F *)p2
{
    return [Line2F signTestedX:tested.x testedY:tested.y
                           p1x:p1.x p1y:p1.y
                           p2x:p2.x p2y:p2.y];
}

+ (CGFloat)signTestedX:(CGFloat)testedX testedY:(CGFloat)testedY
                   p1x:(CGFloat)p1x p1y:(CGFloat)p1y
                   p2x:(CGFloat)p2x p2y:(CGFloat)p2y
{
  return (testedX - p2x) * (p1y - p2y) - (p1x - p2x) * (testedY - p2y);  
}

@end

谁能解释为什么大部分 CPU 时间花在 [NSObject release][NSObject retain] 上?

最佳答案

如果它不知道更好的方法,ARC 将保留方法的所有参数并在方法退出时释放它们(参见 this objc-language mailing list email)。

您应该能够通过使用 __weak__unsafe_unretained 注释 +signTested:p1:p2: 的参数来避免这种情况,per您的需求。

关于iOS 和 Objective-C : most of CPU time is spent in [NSObject release] and [NSObject retain] but class method is not doing any memory operations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469670/

有关iOS 和 Objective-C : most of CPU time is spent in [NSObject release] and [NSObject retain] but class method is not doing any memory operations的更多相关文章

随机推荐