ios - #define vs self.method检查分配

标签 ios objective-c performance cocoa-touch c-preprocessor

我想知道什么是检查对象分配(或其他事情)的正确方法。我自己进行了一些性能测试,发现没有方法调用的检查节省了“大量”的时间。哪种方法被认为是良好的编码?测试和结果如下。

定义:

#define checkUM if (!um) {um =  [[UtilityMaster alloc]init]; }

与方法:
-(void) checkUtility {
    if (!um) {um =  [[UtilityMaster alloc]init]; }
}

检查代码:
int imax = 1000000000;
int i = 0;
IFD100(@"check method")
while (i <= imax) {
    [self checkUtility];
    i++;
}
IFD100(@"check method end")
i = 0;
IFD100(@"check define")
while (i <= imax) {
    checkUM;
    i++;
}
IFD100(@"check define end")

检查1:
2013-06-25 18:36:16.712  check method
2013-06-25 18:36:27.669  check method end  <-- 10.957 secs
2013-06-25 18:36:27.670  check define
2013-06-25 18:36:30.128  check define end  <-- 2.458 secs

检查2:
2013-06-25 18:37:18.900  check method
2013-06-25 18:37:28.678  check method end  <-- 9.778 secs
2013-06-25 18:37:28.679  check define
2013-06-25 18:37:31.136  check define end  <-- 2.457 secs

最佳答案

预处理器宏很难看,尤其是当您尝试使用它们来模拟函数时。所以不要那样做。同时,我怀疑这是否会成为您算法中的瓶颈。如果是这样,则最好制作一个内联函数,并让编译器完成其优化工作。

关于ios - #define vs self.method检查分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17303445/

相关文章:

objective-c - 这是安全/有效的哈希方法实现吗?

ios - UIScrollView 上的 float UIButton

iOS自定义右侧导航栏

arrays - 使用 Repa 并行数组的惯用期权定价和风险

ios - 如何在核心数据中计算一周中每一天的总和

iphone - 触摸开始 :(NSSet *)touches withEvent:(UIEvent *)event doesn't get called

java - 运行 XSL 转换 oracle db,而不是 java 应用程序

r - R如何能够如此快速地对整数序列求和?

ios - 防止 UICollectionView 中的项目 "wrapping"

ios - 检查哪些字典键对应的数组包含某个字符串