Foundation Objective-c:带数组的字典;字典与字典 [英] Foundation Objective-c: Dictionary with array; dict with dict

查看:76
本文介绍了Foundation Objective-c:带数组的字典;字典与字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 NSDictionary,其中包含一个 NSArray 和一个 NSDictionary 的两个子集合:

Suppose I have a NSDictionary with two sub collections of a NSArray and a NSDictionary:

NSMutableDictionary *mkDict(void){
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    NSMutableDictionary *sub=[NSMutableDictionary dictionary];
    NSMutableArray *array= [NSMutableArray array];
    [dict setObject:array forKey:@"array_key"];
    [dict setObject:sub forKey:@"dict_key"];
    return dict;
}

有多种方法可以访问子集合的单个元素,我选择对其中三种进行计时.

There are a multitude of ways to access a single element of the sub-collection, and I choose to time three of them.

第一种方式是通过访问父元素的key来间接访问子元素:

The first way is to indirectly access the subelements by accessing the key of the parent:

void KVC1(NSMutableDictionary *dict, int count){

    for(int i=0; i<count; i++){
        char buf1[40], buf2[sizeof buf1];
        snprintf(buf1,sizeof(buf1),"element %i", i);
        snprintf(buf2, sizeof buf2, "key %i", i);

        [[dict objectForKey:@"array_key"] 
          addObject:
             [NSString stringWithUTF8String:buf1]];
        [[dict objectForKey:@"dict_key"] 
          setObject:[NSString stringWithUTF8String:buf1] 
          forKey:[NSString stringWithUTF8String:buf2]];
    }
}

第二种是使用KeyPath访问:

The second is to use KeyPath access:

void KVC2(NSMutableDictionary *dict, int count){

    for(int i=0; i<count; i++){
        char buf1[40], buf2[sizeof buf1], buf3[sizeof buf1];
        snprintf(buf1,sizeof(buf1),"element %i", i);
        snprintf(buf2, sizeof buf2, "key %i", i);
        snprintf(buf3, sizeof buf3, "dict_key.key %i",i);

        [dict insertValue:
             [NSString stringWithUTF8String:buf1] 
            atIndex:i inPropertyWithKey:@"array_key"];
        [dict setValue:
             [NSString stringWithUTF8String:buf1] 
            forKeyPath:
             [NSString stringWithUTF8String:buf3]];
    }
}

第三个,与第一个类似,是访问一个指向子元素的指针,然后使用该指针:

And the third, similar to the first, is to access a pointer to the sub element, then use that pointer:

void KVC3(NSMutableDictionary *dict, int count){

    NSMutableArray *subArray = [dict objectForKey:@"array_key"];
    NSMutableDictionary *subDict = [dict objectForKey:@"dict_key"];

    for(int i=0; i<count; i++){
        char buf1[40], buf2[sizeof buf1];
        snprintf(buf1,sizeof(buf1),"element %i", i);
        snprintf(buf2, sizeof buf2, "key %i", i);

        [subArray addObject:[NSString stringWithUTF8String:buf1]];
        [subDict 
           setObject:
            [NSString stringWithUTF8String:buf1] 
           forKey:
            [NSString stringWithUTF8String:buf2]];
    }
}

这是时间代码:

#import <Foundation/Foundation.h>
#import <mach/mach_time.h>

// KVC1, KVC2 and KVC3 from above...

#define TIME_THIS(func,times) \
({\
mach_timebase_info_data_t info; \
mach_timebase_info(&info); \
uint64_t start = mach_absolute_time(); \
for(int i=0; i<(int)times; i++) \
func ; \
uint64_t duration = mach_absolute_time() - start; \
duration *= info.numer; \
duration /= info.denom; \
duration /= 1000000; \
NSLog(@"%i executions of line %i took %lld milliseconds", times, __LINE__, duration); \
});

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSMutableDictionary *dict=mkDict();
    NSMutableDictionary *dict2=mkDict();
    NSMutableDictionary *dict3=mkDict();

    TIME_THIS(KVC1(dict,1000),10);
    TIME_THIS(KVC2(dict2,1000),10);
    TIME_THIS(KVC3(dict3,1000),10);

    if([dict isEqualToDictionary:dict2])
        NSLog(@"And they are the same...");
    [pool drain];
    return 0;
}

结果如下:

10 executions of line 256 took 57 milliseconds
10 executions of line 257 took 7930 milliseconds
10 executions of line 258 took 46 milliseconds
And they are the same...

问题:为什么 OS X Snow Leopard/Lion 建议的使用 KeyPaths 的方法如此缓慢?如果将 count 的大小增加到 10,000 或更多,KVC2 会变得无限慢,而其他两种方法线性增加.

Question: Why is the OS X Snow Leopard / Lion suggested method of using KeyPaths so stinking slow? If you increase the size of count to 10,000 or more, KVC2 becomes infinitely slow where the other two methods increase linearly.

我做错了吗?是否有更好的习惯用法来访问字典中子集合的单个元素?

Am I doing something wrong? Is there a better idiom to access a single element of a sub collection in a dictionary?

推荐答案

KVC2()中,你发送

[dict insertValue:[NSString stringWithUTF8String:buf1] 
          atIndex:i
inPropertyWithKey:@"array_key"]; 

该方法的文档说明如下:

The documentation for that method states the following:

方法 insertIn:atIndex: 如果存在就会被调用.如果没有找到相应的脚本 KVC 兼容方法 (insertIn:atIndex:),则此方法调用 mutableArrayValueForKey: 并改变结果.

The method insertIn<Key>:atIndex: is invoked if it exists. If no corresponding scripting-KVC-compliant method (insertIn<Key>:atIndex:) is found, this method invokes mutableArrayValueForKey: and mutates the result.

因为消息被发送到 dictNSDictionary 的一个实例,所以没有 -insertIn:atIndex: 方法,因此 -mutableArrayValueForKey: 被发送.此方法的文档说明如下:

Since the message is being sent to dict, an instance of NSDictionary, there is no -insertIn<Key>:atIndex: method, hence -mutableArrayValueForKey: is sent. The documentation for this method states the following:

返回值一个可变数组代理,提供对键指定的有序对多关系的读写访问.

Return Value A mutable array proxy that provides read-write access to the ordered to-many relationship specified by key.

讨论添加到可变数组中的对象与接收者相关,而从可变数组中删除的对象变得不相关.默认实现识别与 valueForKey: 相同的简单访问器方法和数组访问器方法,并遵循相同的直接实例变量访问策略,但始终返回可变集合代理对象,而不是 valueForKey: 将返回的不可变集合.

Discussion Objects added to the mutable array become related to the receiver, and objects removed from the mutable array become unrelated. The default implementation recognizes the same simple accessor methods and array accessor methods as valueForKey:, and follows the same direct instance variable access policies, but always returns a mutable collection proxy object instead of the immutable collection that valueForKey: would return.

所以发生的事情是在每次迭代中:

  1. 代理可变数组被创建为原始数组的可变副本
  2. 一个对象被添加到代理数组中;
  3. 代理数组将相同的对象添加到原始数组中.

如果你使用 Instruments 来分析你的程序,你会注意到大约 50% 的处理时间花在 -[NSKeyValueSlowMutableArray insertObject:atIndex:] 上——我认为可以安全地假设NSKeyValueSlowMutableArray 是代理数组,它的名字应该是其性能的线索.

If you use Instruments to profile your program, you’ll notice that about 50% of the processing time is spent on -[NSKeyValueSlowMutableArray insertObject:atIndex:] — I think it’s safe to assume that NSKeyValueSlowMutableArray is the proxy array and its name should be a clue of its performance.

这篇关于Foundation Objective-c:带数组的字典;字典与字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆