如果我用一些动态属性编写一个类别而不是说子类化,这在性能方面是否真的重要?
(让我们保持专注并坚持表现,而不是为这篇文章设计模式)
答案 0 :(得分:2)
是的,似乎存在性能差异。我不会说这是很多。这些结果来自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
运行时
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");
使用此代码
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