我想知道什么是正确的方式去检查对象的分配(或其他事情)。我自己做了一些性能测试,发现在没有方法调用的情况下进行检查可以节省“大量”时间。哪种方式被认为是良好的编码?下面的测试和结果。#define vs self.method检查分配
的定义:
#define checkUM if (!um) {um = [[UtilityMaster alloc]init]; }
VS的方法:用于检查
-(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
时差是宏不做的10亿次方法调用。 – rmaddy
顺便说一句 - 你的日志语句是颠倒过来的。您记录“检查定义”,然后测试方法调用。 – rmaddy
是的,我喜欢这个逆转太haha – mrosales