开发者

Is it generally faster to call an instance method rather than a class method?

开发者 https://www.devze.com 2022-12-23 07:57 出处:网络
Today I encountered something strange: I tried to put a utility method into an Utility class as a class method, so that I can simply call that method to g开发者_如何学JAVAenerate me an convenient UIVi

Today I encountered something strange: I tried to put a utility method into an Utility class as a class method, so that I can simply call that method to g开发者_如何学JAVAenerate me an convenient UIView with no hassle (not the imageNamed method but the other, much more complex one). It was horrible. The performance was so incredible bad. Then I made an instance method out of it, created an instance of that class and called the instance method. Performance was suddenly great. That is so strange!

What are your experiences with this kind of stuff?


There's something else going on in your case. Method calls are pretty lightweight, and I doubt there is a noticeable difference in performance between a class method and an instance method. At least, I've never seen a difference between the two in my experience.

Sources of performance degradation are often not what they first appear to be, so I recommend running your application against Instruments or Shark to see where the bottleneck is in this case. My guess is that you are allocating memory in the class method approach that was not allocated in the instance method, which is one of the most expensive operations on the iPhone.

0

精彩评论

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

关注公众号