Obj-C: class methods versus instance methods - difference in efficiency?

For convenience and reusability, I am considering creating a bunch of utility methods inside one class. A class does not require any of its own properties, so I will define methods as methods of the class; I believe there is no real need to have an instance of this class floating around ...

... or is there? Is there a difference in efficiency? (Noticeable?) Or do the methods of calling the class behave as if they were part of the calling class?

+3
source share
4 answers

dandan78 . . , , , , , , .

, C , . , , C, , .

, . Objective-C , . , .

+6

, , , , - , . , - , , .

, - , , , [NSDate date], .

+2

C . Objective-C . , , , , , .

, C, . Objective-C , - , .

+2

, C. :

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

, , , ; .

To return to the original question, you should use the class method, since your methods are not dependent on any state (properties) that the instance may have. Performance is wise, this can only help because using the actual instance means you need to somehow distribute and access that instance, which adds overhead (again, all this is insignificant).

Using the C function will be imperceptibly faster, but you cannot subclass and override this C function as if you were using a class method.

+1
source

All Articles