admin管理员组文章数量:1130349
Messaging
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]);
}
查看打印时间,直接地址调用运行时间的确优于消息发送。
参考:
Objective-C Runtime Programming Guide-Messaging
Objective-C Runtime Reference[msg_send]
Messaging
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]);
}
查看打印时间,直接地址调用运行时间的确优于消息发送。
参考:
Objective-C Runtime Programming Guide-Messaging
Objective-C Runtime Reference[msg_send]
本文标签: Messaging
版权声明:本文标题:Messaging 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/IT/1686741268a31656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论