My program is spending most of its time in objc_msgSend. Does that mean that Objective-C has bad performance?
I have written an application that has a number of custom views and generally draws a lot of lines and bitmaps. Since performance is somewhat critical for the application, I spent a good amount of time optimizing draw performance.
Now, activity monitor tells me that my a开发者_运维技巧pplication is usually using about 12% CPU and Instrument (the profiler) says that a whopping 10% CPU is spent in objc_msgSend
(mostly in drawing related system calls).
On the one hand, I am glad about this since it means that my drawing is about as fast as it gets and my optimizations where a huge success. On the other hand, it seems to imply that the only thing that is still using my CPU is the Objective-C overhead for messages (objc_msgSend
). Hence, that if I had written the application in, say, Carbon, its performance would be drastically better.
Now I am tempted to conclude that Objective-C is a language with bad performance, even though Cocoa seems to be awfully efficient since it can apparently draw faster than Objective-C can send messages.
So, is Objective-C really a language with bad performance? What do you think about that?
No, Objective-C's performance is not that bad. For evidence, I cite @bbum's series of articles on the hand-optimized assembly of objc_msgSend:
- The Roadmap
- Setting the Stage
- The Fast Path
- Method Lookup and some odds and ends
In other words, objc_msgSend
is fast; method dispatch is not your problem. Can you post your profiling information that leads you to believe otherwise? It's possible that you're misreading the analysis, or that you're doing something really strange under the covers, etc.
But I guarantee you that objc_msgSend
is not the problem. If it was, we surely would've heard about it by now. :)
Which way around are you viewing the call Tree (see the checkbox marked "Invert Call Tree")?
If you have the call stack inverted, have you drilled down into objc_msgSend to see where the time is spent?
The "Running Time %" that is shown by default (cntrl-click to change) means 'percentage of time your code was busy' - eg. if you run 1 short fast method and then idle for 10 hours the percentage time for your method, and therefore for objc_msgSend because that is the method that called your method - would be %100 - That doesn't mean that objc_msgSend is slow.
精彩评论