Reputation: 403
For the sake of tidiness and reusability, I'm thinking about putting a bunch of utility methods inside a single class. The class does not require any properties of its own and so I will define the methods as class methods; I figure that's there's no real need to have an instance of this class floating around…
…or is there? Is there a difference in efficiency? (A noticeable one?) Or does calling class methods behave in the same way as if they were part of the calling class?
Upvotes: 4
Views: 971
Reputation: 7707
I made a sample project that compares calling instance methods to class methods or a C function. Here's some example output:
Class methods: Time to complete: 0.00415796
Single instance: Time to complete: 0.00437099
Using Singleton: Time to complete: 0.071667
Using Global: Time to complete: 0.00534797
Using C function: Time to complete: 0.00302202
The primary conclusion you should get from this is that the differences are negligible; use Instruments to profile for actual performance bottlenecks.
To get back to your original question, you should use a class method, since your methods don't depend on any state (properties) that an instance might hold. Performance wise, this can only help because using an actual instance means you have to allocate and access that instance somehow, which adds overhead (again, all this is negligible).
Using a C function will be unnoticeably quicker, but you won't be able to subclass and override that C function like you would a class method.
Upvotes: 2
Reputation: 4145
dandan78 has the correct answer in general. It's always best to measure your code yourself. In fact, for this problem, you should probably just implement it the way you want, and then measure, and only change it if it's significantly slowing down your app.
Gavin is also correct that C functions are more efficient than class or instance methods. But again, unless you're calling these methods millions of times in a tight loop, the overhead of a method vs a C function probably won't matter to your application.
But to answer the question asked, there's basically no difference between class methods and instance methods. In Objective-C, classes are actually objects themselves, and sending messages to them is identical to sending a message to an instance object. Each class basically has a singleton object that class methods are called on.
Upvotes: 7
Reputation: 8200
The most efficient way would be to make them C functions, actually. You'll avoid the overhead of the Objective-C runtime every time you call one of them. Obviously based on the wording of your question, you don't need extra state that you keep around for these utility methods, since you were thinking about making them class methods anyway.
When I keep around utility methods like this, I most of the time make them C functions, because it's cleanest and most efficient. When I might make an Objective-C class with them as methods is if they have something in common with each other where it makes sense to have them organized this way, or if I need to keep state around for them.
Upvotes: 3
Reputation: 13854
As in all cases regarding efficiency-related dilemmas, your best bet is to do what makes sense from a coding point of view -- make the code as intuitive and reader-friendly as you can. If it turns out that something is not as fast as it needs to be, profile and optimize accordingly.
And although I don't expect there to be any difference in this case, you can make a class and a instance version of a method, run it a million times in a loop preceded and followed by [NSDate date]
calls, then compare the times.
Upvotes: 3