Created
February 23, 2016 23:13
-
-
Save mayoff/fb3698c1c45cd9b1426a to your computer and use it in GitHub Desktop.
KVO 100x slowdown in El Capitan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Subject: NSObject | |
@property double a; | |
@property long b; | |
@property NSObject* c; | |
@property NSObject* d; | |
@property NSObject* e; | |
@property NSObject* f; | |
@property NSObject* g; | |
@property NSObject* h; | |
@property NSObject* i; | |
@property double j; | |
@property double k; | |
@property double l; | |
@property double m; | |
@property double n; | |
@property long o; | |
@end | |
@implementation Subject | |
@end | |
@interface Observer : NSObject | |
@end | |
@implementation Observer | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { | |
} | |
@end | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSArray *keys = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o"]; | |
Observer *observer = [Observer new]; | |
NSMutableArray *positions = [NSMutableArray array]; | |
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); | |
static const int Max = 1000; | |
for (int i = 0; i < Max; ++i) { | |
Subject *position = [Subject new]; | |
for (NSString *key in keys) { | |
[position addObserver:observer forKeyPath:key options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:0]; | |
} | |
[positions addObject:position]; | |
} | |
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent(); | |
printf("%.6f\n", (end - start)); | |
exit(0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment