简体   繁体   English

NSMutableArray -init vs. + arrayWithCapacity:

[英]NSMutableArray -init vs. +arrayWithCapacity:

I have two functions which one should i use? 我有两个功能,我应该使用哪一个? Please explain the difference. 请解释一下差异。

A: A:

- (NSMutableArray *)FunctionA:(int)count {

    NSMutableArray *a = [[NSMutableArray alloc] init];

 for (int i = 0; i < count; i++) {
  [a addObject:[NSNumber numberWithInt:0] ];
    }

    return [a autorelease];
}

B: B:

-(NSMutableArray *)FunctionB:(int)count {

 NSMutableArray *b = [NSMutableArray arrayWithCapacity:count];

 for (int i=0;i<count; i++){  
  [b addObject:[NSNumber numberWithInt:0] ];  
 }

 return b;  //  or [b autorelease] ?
}

The first creates a mutable array without specifying a capacity. 第一个创建可变数组而不指定容量。 This causes the array to have to grow when you add items. 这会导致在添加项目时数组必须增长。 Internally, this is probably heavily optimized to occur "chunks at a time" but it's still necessary to grow the array and allocate more space when adding items. 在内部,这可能经过大量优化,一次发生“块”,但仍需要增加数组并在添加项目时分配更多空间。

The second gives the array a hint (you're probably going to need "this much" room) to avoid the overhead of growing the array when adding a known number of objects. 第二个给数组一个提示(你可能需要“这么多”的空间),以避免在添加已知数量的对象时增加数组的开销。 Of course it will still grow larger if needed (as if you hadn't specified a capacity). 当然,如果需要,它仍然会变大(就像你没有指定容量一样)。 You should use this approach if you already know the count ahead of time. 如果您已经提前了解计数,则应使用此方法。 It's faster with a large count. 计数很大,速度更快。

This has a downside if you haven't measured before optimizing, however: If you're creating a mutable array with a very high capacity but not always using that capacity, you incur the penalty of allocating all that space for nothing. 如果您在优化之前没有测量过,那么这有一个缺点:如果您正在创建一个具有非常高容量但不总是使用该容量的可变阵列,则会产生分配所有空间的代价。

Also, you don't autorelease B (as you commented out) because you didn't create the mutable array with init - you used a convenience method which did it itself, which means you're not responsible for releasing it. 此外,你没有自动释放B(你注释掉了),因为你没有用init创建可变数组 - 你使用了一个方便的方法,它自己做了,这意味着你不负责释放它。 As I mentioned in a comment to another answer to your question, you can also create the array with: 正如我在对您的问题的另一个答案的评论中提到的,您还可以使用以下命令创建数组:

[[NSMutableArray alloc] initWithCapacity:capacity];

... then release it when ready. ...然后在准备好后释放它。 This gives you greater control over memory usage than using the autorelease pool, which is an important consideration on the iPhone platform. 与使用自动释放池相比,这使您可以更好地控制内存使用,这是iPhone平台上的一个重要考虑因素。

Remember, though: measure first, then optimize if necessary. 但请记住:先测量,然后根据需要进行优化。

Mutable objects still need to allocate space so that will allocate a default amount for say 10 objects. 可变对象仍然需要分配空间,以便为10个对象分配默认值。 If you add an 11th, the mutable array will have to allocate new memory, copy the items over to the new memory, and free the old memory. 如果添加第11个,则可变数组将必须分配新内存,将项目复制到新内存,并释放旧内存。

This is normally so fast you won't even notice but it could slow it down. 这通常是如此之快,你甚至不会注意到,但它可以减慢它。 Creating the mutable array with a size creates the array of the specified size initially so hopefully less resizing will occur. 创建具有大小的可变数组最初会创建指定大小的数组,因此希望减少调整大小。

In the first example, you must manage the memory of the array, since you create it using +alloc and -init (which is why you need to send -autorelease to it). 在第一个示例中,您必须管理数组的内存,因为您使用+alloc-init创建它(这就是您需要向其发送-autorelease的原因)。

In the second example, you do not have to manage the memory of the array, since it is returned to you autoreleased (because you created it using a convenience method). 在第二个示例中,您不必管理数组的内存,因为它会自动释放返回给您(因为您使用便捷方法创建它)。 Also since you specify the desired size of the array up front, it is likely to be more efficient. 此外,由于您预先指定了所需的阵列大小,因此可能更高效。

If you want to return an autoreleased array, then the second option would probably be more preferable, since +arrayWithCapacity: will return an already autoreleased array. 如果你想返回一个自动释放的数组,那么第二个选项可能更可取,因为+arrayWithCapacity:将返回一个已经自动释放的数组。 Also since the array returned to you is already autoreleased, you do not have to send -autorelease to it yourself. 此外,由于返回给你的数组已经自动释放,你不必自己发送-autorelease

If you have any more concerns with memory management, then reading the Apple Memory Management Guidelines is a must. 如果您对内存管理有任何疑虑,那么必须阅读Apple 内存管理指南

I'd use B: 我用B:

The pluses I see with it are 我看到它的优点是

  • The array will not need to resize as it grows larger 随着数组变大,数组不需要调整大小
  • arrayWithCapacity: autoreleases for you so you don't have to, this improves code readability imho arrayWithCapacity:为您自动释放所以您不必,这提高了代码可读性imho

I hope that helps. 我希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM