0 - strong start: 1551270692.478 (0.000)
1 - strong fill array: 1551270692.860 (0.382)
2 - strong free object: 1551270692.860 (0.000)
3 - strong enum array: 1551270693.080 (0.219)
4 - strong free array: 1551270693.334 (0.254)
5 - weak start: 1551270693.334 (0.000)
6 - weak fill array: 1551270693.857 (0.523)
7 - weak free object: 1551270693.881 (0.024)
8 - weak enum array: 1551270694.074 (0.193)
9 - weak free array: 1551270694.323 (0.249)
1 - strong fill array: 1551270692.860 (0.382)
2 - strong free object: 1551270692.860 (0.000)
3 - strong enum array: 1551270693.080 (0.219)
4 - strong free array: 1551270693.334 (0.254)
5 - weak start: 1551270693.334 (0.000)
6 - weak fill array: 1551270693.857 (0.523)
7 - weak free object: 1551270693.881 (0.024)
8 - weak enum array: 1551270694.074 (0.193)
9 - weak free array: 1551270694.323 (0.249)
@interface KRStringContainer : NSObject
@property (nonatomic, strong) NSString *str;
@end
@interface KRWeakRefContainer : NSObject
@property (nonatomic, weak) KRStringContainer *cont;
@end
@interface KRStrongRefContainer : NSObject
@property (nonatomic, strong) KRStringContainer *cont;
@end
+(void)test_weak_vs_strong{
const NSInteger max_count = 1000000;
NSMutableDictionary<NSString*, NSDate*> *dict_m = [NSMutableDictionary dictionary];
NSMutableArray<KRStrongRefContainer*> *arr_strong_m = [NSMutableArray arrayWithCapacity:max_count];
NSMutableArray<KRWeakRefContainer*> *arr_weak_m = [NSMutableArray arrayWithCapacity:max_count];
KRStringContainer *cont_for_weak = [[KRStringContainer alloc] init];
cont_for_weak.str = @"weak";
KRStringContainer *cont_for_strong = [[KRStringContainer alloc] init];
cont_for_strong.str = @"strong";
dict_m[@"0 - strong start"] = [NSDate date];
for( int i=0; i<max_count; ++i){
[arr_strong_m addObject:[KRStrongRefContainer createWith:cont_for_strong]];
}
dict_m[@"1 - strong fill array"] = [NSDate date];
cont_for_strong = nil;
dict_m[@"2 - strong free object"] = [NSDate date];
for( int i=0; i<max_count; ++i){
KRStringContainer *cont = arr_strong_m[i].cont;
cont = cont;
}
dict_m[@"3 - strong enum array"] = [NSDate date];
[arr_strong_m removeAllObjects];
dict_m[@"4 - strong free array"] = [NSDate date];
dict_m[@"5 - weak start"] = [NSDate date];
for( int i=0; i<max_count; ++i){
[arr_weak_m addObject:[KRWeakRefContainer createWith:cont_for_weak]];
}
dict_m[@"6 - weak fill array"] = [NSDate date];
cont_for_weak = nil;
dict_m[@"7 - weak free object"] = [NSDate date];
for( int i=0; i<max_count; ++i){
KRStringContainer *cont = arr_weak_m[i].cont;
cont = cont;
}
dict_m[@"8 - weak enum array"] = [NSDate date];
[arr_weak_m removeAllObjects];
dict_m[@"9 - weak free array"] = [NSDate date];
NSArray *keys = [dict_m.allKeys sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]]];
NSString *k_prev = nil;
for (NSString *k in keys) {
if( k_prev==nil ){
NSLog(@"%@: %.3lf\t(%.3lf)", k, [dict_m[k] timeIntervalSince1970], 0.0);
}else{
NSLog(@"%@: %.3lf\t(%.3lf)", k, [dict_m[k] timeIntervalSince1970]
, [dict_m[k] timeIntervalSince1970]-[dict_m[k_prev] timeIntervalSince1970]);
}
k_prev = k;
}
}
Комментариев нет:
Отправить комментарий