xcode 初始化 NSMutableArray 有无容量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11848059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:10:06  来源:igfitidea点击:

Initializing NSMutableArray with and without capacity

objective-cxcodensmutablearray

提问by Nom nom

Possible Duplicate:
NSMutableArray initWithCapacity nuances
Objective-c NSArray init versus initWithCapacity:0

可能的重复:
NSMutableArray initWithCapacity 细微差别
Objective-c NSArray init 与 initWithCapacity:0

What is the difference between following line of code? what is the exact advantage and disadvantage? suppose next I will do 3 addObject operation.

以下代码行有什么区别?确切的优点和缺点是什么?假设接下来我将执行 3 个 addObject 操作。

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 5];
NSMutableArray *array = [[NSMutableArray alloc] init];

回答by JeremyP

Functionally both statements are identical.

在功能上,这两个语句是相同的。

In the first case, you are giving the run time a hint that you will soon be adding five objects to the array so it can, if it likes, preallocate some space for them. That means that the first five addObject:invocations may be marginally faster using the first statement.

在第一种情况下,您向运行时提示您将很快向数组添加五个对象,以便它可以(如果愿意)为它们预先分配一些空间。这意味着addObject:使用第一个语句前五个调用可能会稍微快一点。

However, there's no guarantee that the run time does anything but ignore the hint. I never use initWithCapacity:myself. If I ever get to a situation where addObject:is a significant performance bottleneck, I might try it to see if things improve.

但是,除了忽略提示之外,不能保证运行时执行任何操作。我从不使用initWithCapacity:自己。如果我遇到addObject:严重的性能瓶颈,我可能会尝试看看情况是否有所改善。

回答by El Developer

Regularly the difference with object oriented languages and arrays of varying sizes is: the overhead you will get and the page faults at the memory level.

通常,面向对象语言和不同大小的数组的区别在于:您将获得的开销和内存级别的页面错误。

To put this in another way imagine you have an object that requests 5 spaces in memory (just like your first example) and the second object doesn't reserve any space. Therefore when an object needs to be added to the first, there will already be space in memory for it to just fall in, on the other hand the non-allocated-object will first have to request for space on memory then add it to the array. This doesn't sound so bad at this level but when your arrays increase in size this becomes more important.

换句话说,假设您有一个对象在内存中请求 5 个空间(就像您的第一个示例一样),而第二个对象不保留任何空间。因此,当需要将一个对象添加到第一个对象时,内存中已经有空间供它落入,另一方面,未分配的对象将首先必须请求内存空间,然后将其添加到大批。这在这个级别上听起来还不错,但是当您的数组大小增加时,这变得更加重要。

From Apple's documentation:

来自Apple 的文档

arrayWithCapacity:

arrayWithCapacity:

Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects. ... The initial capacity of the new array. Return Value A new NSMutableArray object with enough allocated memory to hold numItems objects.

创建并返回一个 NSMutableArray 对象,该对象具有足够的分配内存来最初保存给定数量的对象。... 新阵列的初始容量。返回值 一个新的 NSMutableArray 对象,具有足够的分配内存来保存 numItems 对象。