views:

76

answers:

1

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 generate 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?

+4  A: 

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.

Brad Larson
Indeed there was a slight difference. I didn't see it. Once I used objectWithFooBar and in the other case I did an alloc init couple. But: Isn't the objectWithFooBar stuff internally just doing the alloc+init for me? Should actually be the same thing: Allocating memory. In detail I created an UIImage.
dontWatchMyProfile