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