roovent icon

Function Calling Comparison

roovent | PRO | 08/01/18 09:50:36 PM UTC | 0 ⭐ | 7601 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

// Code in this article:
// https://it.toolbox.com/blogs/hohlecow/bypassing-objective-cs-message-passing-mechanism-for-speed-052708
 
#import <Cocoa/Cocoa.h> 
@interface Fib : NSObject { 
} 
- (long long) messageFib: (NSUInteger) number;
- (long long) impFib: (NSUInteger) number; 
- (long long) cFib: (NSUInteger) number; 
@end 
 
@implementation Fib 
 
// message passing implementation of fib 
- (long long) messageFib: (NSUInteger) number { 
    return (number < 3) ? 1 : [self messageFib: number - 1] + [self messageFib: number - 2]; 
} 
 
// imp implementation of fib 
- (long long) impFib: (NSUInteger) number { 
    static long long (*impFibIMP)(id, SEL, NSUInteger) = nil; 
    if (!impFibIMP) { 
        impFibIMP = (long long (*)(id, SEL, NSUInteger)) [self methodForSelector: _cmd]; 
    } 
    return (number < 3) ? 1 : impFibIMP(self, _cmd, number - 1) + impFibIMP(self, _cmd, number - 2); 
} 
 
// c implementation of fib 
long long cFib(NSUInteger number) { 
    return (number < 3) ? 1 : cFib(number - 1) + cFib(number - 2); 
} 
 
// method wrapper for c implementation of fib 
- (long long) cFib: (NSUInteger) number { 
    return cFib(number); 
} 
 
@end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎