【问题标题】:Optimize function with creating NSDictionarry通过创建 NSDictionarry 优化功能
【发布时间】:2013-10-25 07:13:53
【问题描述】:

我正在尝试优化返回 NSMutableDictionary 的函数:

 -(NSMutableDictionary *)getValuses{

    NSNumber *n1 = [NSNumber numberWithInt:-1];
    NSNumber *n2 = [NSNumber numberWithInt:-1];
    NSNumber *n3 = [NSNumber numberWithInt:-1];
    NSNumber *n4 = [NSNumber numberWithInt:-1];
    NSNumber *n5 = [NSNumber numberWithInt:-1];

    if (self.k1)
        n1 = self.k1;
    if (self.k2)
        n2 = self.k2;
    if (self.k3)
        n3 = self.k3;
    if (self.k4)
        n4 = self.k4;
    if (self.k5)
        n5 = self.k5;

    NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:n1,[NSNumber numberWithInt:2],n2,[NSNumber numberWithInt:3],n3,[NSNumber numberWithInt:4],n4,[NSNumber numberWithInt:5],n5,[NSNumber numberWithInt:6], nil];

    return dictionary;
}  

我在循环中运行这个函数超过 1 000 000 次,所以任何优化都是好的。它可以工作,但我希望它工作得更快。

【问题讨论】:

    标签: ios objective-c xcode optimization nsmutabledictionary


    【解决方案1】:

    你真的需要 -1 值的字典吗? 如果你只是这样做,你可以避免所有“if/then”的东西(我听说它对于 cpu 来说可能很慢)

        NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:k1,[NSNumber numberWithInt:2],k2,[NSNumber numberWithInt:3],k3,[NSNumber numberWithInt:4],k4,[NSNumber numberWithInt:5],k5,[NSNumber numberWithInt:6], nil];
        // then you can do things like this
        id obj = [dictionary objectForKey:@2];
        if (obj)
            NSLog(@"dict with good values");
        else
            NSLog(@"old dict with -1");
    

    【讨论】:

    • 你说得对,我真的不需要 -1 值,我只是认为 nill 值会被拒绝。这种方法更快。 (在循环范围内测试 1.892.419 需要 1 分钟 9 秒而不是 1 分钟 40 秒)
    【解决方案2】:
    -(NSMutableDictionary *)getValuses{
    
        NSNumber *n1 = [NSNumber numberWithInt:-1];
       NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:(self.k1)? self.k1:n1,[NSNumber numberWithInt:2],(self.k2)? self.k2:n1,[NSNumber numberWithInt:3],(self.k3)? self.k3:n1,[NSNumber numberWithInt:4],(self.k4)? self.k4:n1,[NSNumber numberWithInt:5],(self.k5)? self.k5:n1,[NSNumber numberWithInt:6], nil];
    
        return dictionary;
    }
    

    试试上面的代码....

    【讨论】:

    • 虽然我确信这种方法会更快,但事实证明它需要相同的时间(Testen on loop with range 1.892.419)
    【解决方案3】:

    您可以尝试这样的事情(未测试):

    -(NSMutableDictionary *)getValuses {
        NSNumber *n = [NSNumber numberWithInt:-1];
        NSMutableDictionary * dictionary = 
            [[NSMutableDictionary alloc] initWithObjectsAndKeys: 
                self.k1 ? self.k1 : n,[NSNumber numberWithInt:2],
                self.k2 ? self.k2 : n,[NSNumber numberWithInt:3]...
        return dictionary;
    }
    

    【讨论】:

    • 哈哈我的回答 :P :p
    • @Raon 是的,几乎一样 :)
    【解决方案4】:

    您可以减少创建的NSNumber 对象的数量,并且您可以使用新的文字语法至少使代码更短且更易于阅读。您还可以将属性访问次数减半。这是否会对性能产生重大影响,您必须弄清楚。

    -(NSMutableDictionary *)getValuses
    {
       NSNumber *n = @(-1);
    
       return @{ @2: (self.k1 ?: n), @3: (self.k2 ?: n), @4: (self.k3 ?: n),
                 @5: (self.k4 ?: n), @6: (self.k5 ?: n)
               };
    }
    

    (表达式a ?: ba ? a : b 的简写但是 a 只会被计算一次,因此属性访问次数减半。)

    【讨论】:

      【解决方案5】:

      试试这个! dispatch_appy 方法用于创建循环并在并发队列中执行代码。这是执行大循环的更好方法。

      dispatch_apply blocks - Apple Documentation

      我没有编译它,但它应该可以工作。希望对您有所帮助。祝你好运!

      -(NSMutableDictionary *)getValuses{
      
          //add the values here
          NSMutableArray *array = [@[self.k1,self.k2,self.k3,self.k4,self.k5]mutableCopy];
          NSMutableDictionary * dictionary = [@{}mutableCopy];
      
          //this kind of block is better for big loops...
          size_t count = array.count;
          dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      
          dispatch_apply(count, queue, ^(size_t i) {
              id value = [array objectAtIndex:i];
              [dictionary setObject:value?value:(@-1) forKey:@(i)];
          });
      
          return dictionary;
      }
      

      【讨论】:

      • 4 次迭代循环如何成为大循环?
      • 这只是一个例子.. 显然我只写了 4 个元素,伙计。想象一下它必须为数百个元素执行此任务,然后使用 dispatch_apply 方法。
      猜你喜欢
      • 1970-01-01
      • 2012-03-26
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多