0

I know when to use class method and when to use instance method (you should create instance methods when you need code that operates on a specific instance of an object. You create a class method when you need to do something that involves that class in general but probably doesn't operate on any specific objects of that class ). But how to decide that whether to use class method or instance method if i have both the options ? And what are the factors (efficiency ,speed etc) responsible for selecting one type of method ?

D.Dogra
  • 37
  • 1
  • 1
    Possible duplicate of [What is the difference between class and instance methods?](http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods) – Vakas Feb 04 '16 at 06:58
  • I’m marking this as closed as this has been already asked and replied. – Vakas Feb 04 '16 at 06:58
  • When do you have the choice? Do you want to do something really stupid, like passing an instance to a class method? "How to decide" usually involves using your brain. – gnasher729 Feb 04 '16 at 07:19
  • 1
    There is unlikely to be any appreciable difference in speed – Paulw11 Feb 04 '16 at 07:29
  • @downvoters i am asking about the execution speed of the methods [This link](http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods) doesn't explain about the execution speed and internal operating logic.. Please don't down vote . Help me !! – D.Dogra Feb 08 '16 at 11:34

1 Answers1

0
@interface MyMethod : NSObject

//Instance Method
-(int)objAddTwoNum:(int)n1 with:(int)n2;

//Class Method
+(int)clasAddTwoNum:(int)n1 with:(int)n2;

@end

@implementation MyMethod
-(int)objAddTwoNum:(int)n1 with:(int)n2;
{
    int sum = n1+n2;
    return sum;
}

+(int)clasAddTwoNum:(int)n1 with:(int)n2;
{
    int sum = n1+n2;
    return sum;
}
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {

        //Calculating Execution time for Object method
        NSDate *startObj = [NSDate date];
        [[MyMethod new]objAddTwoNum:30 with:40];
        NSDate *finishObj = [NSDate date];

        NSTimeInterval executionTimeObj = [finishObj timeIntervalSinceDate:startObj];

        NSLog(@"Execution Time OBJ operation: %f", executionTimeObj);



        //Calculating Execution time for Class method
        NSDate *startClass = [NSDate date];
        [MyMethod clasAddTwoNum:30 with:40];

        NSDate *finishClass = [NSDate date];
        NSTimeInterval executionTimeClass = [finishClass timeIntervalSinceDate:startClass];

        NSLog(@"Execution Time CLass operation: %f", executionTimeClass);

    }
    return 0;
}

Log is showing class method took less time to execute

2016-02-04 15:17:01.236 HDMethods[43278:1031682] Execution Time OBJ operation: 0.000028
2016-02-04 15:17:01.236 HDMethods[43278:1031682] Execution Time CLass operation: 0.000002
Program ended with exit code: 0

So I think in this case class method is faster

Will try for different methods later.. Happy Coding! :)

HDdeveloper
  • 4,396
  • 6
  • 40
  • 65