简体   繁体   English

运行时属性和实际属性之间的性能差异是什么?

[英]What's the performance difference between runtime property and real properties?

Does it really matter performance-wise if I write a category with some dynamic properties instead of say subclassing? 如果我编写具有一些动态属性的类别而不是说子类化,那么对性能而言真的有意义吗?

(let's keep focus and stick to performance and not design patterns for this post) (让我们保持专注并坚持表现,而不是本文的设计模式)

Yes, it seems like there's a performance difference. 是的,似乎存在性能差异。 I wouldn't say that it is by a lot. 我不会说很多。 These results is from an iPad 3. 这些结果来自iPad 3。

Spent 0.00033 seconds on writing ivar
Spent 0.00010 seconds on reading ivar
Spent 0.00279 seconds on writing ordinary
Spent 0.00200 seconds on reading ordinary
Spent 0.00835 seconds on writing runtime
Spent 0.00763 seconds on reading runtime

When running this 当运行这个

Test *test = [Test new];
int iterations = 10000;

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        test->ivar = @"foo";
    }
}, @"writing ivar");

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        test->ivar;
    }
}, @"reading ivar");

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        test.ordinary = @"foo";
    }
}, @"writing ordinary");

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        [test ordinary];
    }
}, @"reading ordinary");

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        test.runtime = @"foo";
    }
}, @"writing runtime");

logTimeSpentExecutingBlock(^{
    for(int i = 0; i < iterations; i++) {
        [test runtime];
    }
}, @"reading runtime");

With this code 有了这个代码

void logTimeSpentExecutingBlock(dispatch_block_t block, NSString *label) {
    NSTimeInterval then = CFAbsoluteTimeGetCurrent();
    block();
    NSTimeInterval now = CFAbsoluteTimeGetCurrent();
    NSLog(@"Spent %.5f seconds on %@", now - then, label);
}

@interface Test : NSObject {
@public
    NSString *ivar;
}
@property (nonatomic, strong) NSString *ordinary;
@end

@interface Test (Runtime)
@property (nonatomic, strong) NSString *runtime;
@end

@implementation Test

- (void)setOrdinary:(NSString *)ordinary
{
    // the default implementation checks if the ivar is already equal
    _ordinary = ordinary;
}

@end

@implementation Test (Runtime)

- (NSString *)runtime {
    return objc_getAssociatedObject(self, @selector(runtime));
}

- (void)setRuntime:(NSString *)string {
    objc_setAssociatedObject(self, @selector(runtime), string, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Restkit中的“属性”和“属性”有什么区别 - What's the difference between “attribute” and “property” in restkit @property和“看起来”像属性的方法的getters / setters有什么区别? - What is the difference between an @property and getters/setters for methods that “look” like properties? 属性观察者和属性包装器之间的主要区别是什么? - What's the main difference between property observers and property wrappers? AVCaptureDevice的hasFlash和hasTorch属性之间有什么区别? - What is the difference between AVCaptureDevice’s hasFlash and hasTorch properties? 委托属性声明中'weak'和'assign'之间的区别是什么 - What's the difference between 'weak' and 'assign' in delegate property declaration ARC:成员变量和属性之间有什么区别? - ARC: what's the difference between a member variable and property? SKNode的position属性和GKAgent2d的position属性有什么区别 - What is the difference between SKNode's position property and GKAgent2d's position property 运行时标题和跳板标题之间有什么区别? - What is the difference between runtime headers and springboard headers? 这些属性之间有什么区别? - What's the different between these properties? “playbackBufferFull”和“playbackBufferEmpty”属性有什么区别? - What is the difference between “playbackBufferFull” and “playbackBufferEmpty” properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM