Cocoa 的 NSMutableArray 稀疏吗?

发布于 2024-11-19 13:09:53 字数 85 浏览 5 评论 0原文

如果我创建一个 NSMutableArray 可能最多有 2^16 个元素,但大部分是空的,我会浪费空间还是 NSMutableArray 实现为稀疏数组?

If I create an NSMutableArray that might have up to 2^16 elements, but will mostly be empty, will I be wasting space or is NSMutableArray implemented as a sparse array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

负佳期 2024-11-26 13:09:54

不,NSArrayNSMutableArray 都不是稀疏数组。如果您有一个包含 5000 个条目的数组,除了 4999 之外的所有内容都设置为 [NSNull null],它仍然占用 5000 个条目的空间。

同样,NSPointerArray 将有 5000 个条目的空间,其中除索引 4999 之外的所有条目都是 NULL

我使用 NSMutableDictionary 开发了一个稀疏数组对象正如 OMZ 所描述的。这样一来,单个条目就只有空间了。然而,这个空间同时保存索引和对象,并且存在将索引值转换为NSNumber的开销。因此,尽管它们可以在 NSArrayNSMutableArray 可以使用的任何地方使用,但会带来性能损失。这是经典的速度/空间权衡。

请参阅https://github.com/LavaSlider/DSSparseArray

No neither NSArray nor NSMutableArray are sparse arrays. If you have an array with 5000 entries with everything except 4999 set to [NSNull null] it is still taking the space of 5000 entries.

Similarly, an NSPointerArray will have the space for 5000 entries with all the entries NULL except index 4999.

I developed a sparse array object using an NSMutableDictionary as described by OMZ. With this there is only space for the one entry. This space, however, holds both the index and object, and there is the overhead of converting the index values to NSNumbers. So although they can be used anyplace an NSArray or NSMutableArray can be there would be a performance penalty. This is a classic speed / space tradeoff.

See https://github.com/LavaSlider/DSSparseArray

鹿港小镇 2024-11-26 13:09:54

NSArray 对象是静态的(或不可变的),因为它必须在创建它时填充,可以使用 -initWithObjects、+arrayWithObjects,或者使用 -initWithArray 等已经存在的数组的内容。以后无法添加对象。

有一个具体的可变子类(称为 NSMutableArray),它允许根据需要动态添加和删除对象。但是,当您在空状态下初始化它时(通过 -initWithCapacity: 或 +arrayWithCapacity:),您指定的初始长度只是一个提示(创建的数组有足够的内存来容纳该数量的对象),但是它可以根据需要进行扩展。所以是的,在这种情况下,它将是一个稀疏数组。

最好的,

An NSArray object is static (or immutable) in the sense that it must be populated at the moment you create it, either by using -initWithObjects, +arrayWithObjects, or by using the contents of an already existing array with -initWithArray, etc. You cannot add objects later on.

There is a concrete mutable subclass (called NSMutableArray) which allows for adding and removing objects dynamically as needed. However when you initialize it in an empty state (either by -initWithCapacity: or +arrayWithCapacity:) what you specify as the initial length is just a hint (the array is created with enough memory to hold that number of objects), howerver it can be expanded as necessary. So yes, in this case, it'll be a sparse array.

Best,

千柳 2024-11-26 13:09:53

NSArray 中的元素不能为空,并且没有“默认”值。要表示 nil,通常会使用单例 [NSNull null],它仍然是对对象的引用,因此会消耗内存(指针)。我会考虑使用带有数字 (NSNumber) 键的 NSDictionary (或 NSMutableDictionary)。

Elements in an NSArray can't be empty and there's no "default" value. To represent nil, you'd usually use the singleton [NSNull null], which is still a reference to an object so it consumes memory (the pointer). I'd consider using NSDictionary (or NSMutableDictionary) with numeric (NSNumber) keys instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文