开发者

performSelector vs direct call performance

Is there any significant difference in performance when you call

[someObject performSelector:@selector(testMethod:) withObject:anotherObje开发者_运维百科ct];

vs

[someObject testMethod:anotherObject];

?


The first causes an extra call to objc_msgSend() that isn't necessary in the second case.

The performance difference is unlikely to remotely matter unless you are calling said method as quickly as you possibly can many 10s of thousands of times and you aren't doing any significant work in testMethod:.

I.e. don't worry about it unless you measure an actual performance problem.


Interesting fact, performing a selector with a delay of 0 causes that method to be called at the top of the next run loop of the app. You can use that to delay certain events that occur frequently (used a lot in optimizations of UI, like images that get reloaded in a UIScrollView)


No there isn't any performance hit that I am aware of, and if there is any it is not significant.


I’ve come across an important difference when passing data to another view controller in prepareForSegue.

using:

[viewController performSelector:@selector(aMethod:) withObject:anObject];

aMethod is called AFTER viewDidLoad and viewWillAppear of the destination viewController.

using:

[viewController aMethod: anObject] ;

aMethod is called BEFORE viewDidLoad and viewWillAppear of the destination viewController.

So if you’re sending data important for the setup of the destination viewController, use the second way.


There is a lot difference in above both methods. I was trying to get animation of Two buttons coming from right side and stops at center but the second button was coming with 0.3 second delay. Now the main point comes here. I was using one animation method for both of 2 buttons. Now i wanted that when I click Finish button, then both buttons should go to left and again New buttons come. This was fine till reading.

Now when i was writing method for Finish button click. I was performing going out of buttons Animation first and then coming in buttons, but when I used the Above second method i.e. [someObject testMethod:anotherObject]; then what happens is I was not able to see the Going out Animation and directly coming in animation of buttons was shown.

Here actually comes the use of first method i.e. [someObject performSelector:@selector(testMethod:) withObject:anotherObject withDelay:delay];

The reason I found was when I click the Finish button the animation runs in different thread and the other code runs in different thread so the going out action was performed in another thread and coming in was performed in another thread. So first thread was not shown.

After using first method with Delay time of total animation. I achieved my goal. So both methods have their own significance.


For my experience,there are two differences:

  1. The first one can add afterDelay:(CGFloat)seconds, and this is the only case I use the first one.

       [someObject performSelector:@selector(testMethod:) withObject:anotherObject  afterDelay:1.0];
    
  2. The second one, you need to define it in someObject.h. Otherwise, you will get a compile warning.


The answer is that they are exactly the same.

There are two really good articles one from Mike Ash, where he explains the objc_msgSend():

  • http://www.mikeash.com/pyblog/friday-qa-2012-11-16-lets-build-objc_msgsend.html

And an another one from Tom Dalling who is explaining that perform selector is calling objc_msgSend().

  • http://tomdalling.com/blog/cocoa/why-performselector-is-more-dangerous-than-i-thought/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜