Obective-C通过消息(Message)发送给消息接收对象(receiving object)来调用方法(Method),消息直到运行时才会绑定方法。
消息发送转化为objc_msgSend函数调用:
1、首先找到方法实现程序位置
2、然后把方法实现程序位置和方法参数传递给消息接收对象执行
3、返回值(return value)
消息发送过程:
1、首先检查消息接收对象的缓存
2、然后检查消息接收对象的分发表(dispatch table)
3、最后通过isa指针找到父类(super class),重复1和2步骤直到找到方法位置或者到达根类(root class)
通过方法地址绕过动态绑定直接调用方法:
- (void)callMethodByAddress {// 通过方法地址调用
void (*setter)(id, SEL, BOOL);// 方法Method定义,id是消息接收者,SEL是方法selector,BOOL方法参数
setter = (void(*)(id, SEL, BOOL))[self methodForSelector:@selector(setFilled:)];// 获得方法地址
NSDate *start = [NSDate date];
for (int i=0; i<100000; i++) {
setter(self, @selector(setFilled:), YES);// 直接地址调用
}
NSLog(@"%f", [[NSDate date] timeIntervalSinceDate:start]);
start = [NSDate date];
for (int i=0; i<100000; i++) {
[self setFilled:YES];// 消息发送
}
NSLog(@"%f", [[NSDate date] timeIntervalSinceDate:start]);
}
查看打印时间,直接地址调用运行时间的确优于消息发送。
参考: