NSMutableArray initWithCapacity细微差别 [英] NSMutableArray initWithCapacity nuances

查看:428
本文介绍了NSMutableArray initWithCapacity细微差别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有建议如何最好初始化一个NSMutableArray时,说到容量?该文档提到...即使您在创建数组时指定大小,指定的大小也被视为一个提示;数组的实际大小仍为0。所以...

Does anyone have advice on how to best initialize an NSMutableArray when it comes to dictating the capacity? The documentation mentions that "...even though you specify a size when you create an array, the specified size is regarded as a "hint"; the actual size of the array is still 0." So...

1)如果我的init比我通常使用的容量大,我不必担心浪费内存吗?

1) If I init with a greater capacity than I typically use, do I not have to worry about wasted memory?

2)如果我初始化的容量通常低于我使用的容量,我不得不担心更重的处理时间分配更多的内存来容纳额外的元素?

2) If I init with a capacity typically lower than what I use, do I have to worry about heavier processing time allocating more memory to hold the extra elements?

这个初始化的容量对这种数据类型的性能/内存使用有何影响?

Just how impacting is this initialized capacity to the performance/memory usage of this data type?

推荐答案

任何空间被浪费给给太大的容量实际上是一个实现细节,苹果故意不公开,我猜。 NSMutableArray是一个类集群,这意味着你不会实际获取NSMutableArray的实例,但是一些其他的,专门的类遵循同一个接口。和苹果不告诉你哪个类被返回在哪种情况下,以及它的行为。所以这里很难给出真正的建议。

Whether any space is wasted by giving too big a capacity is actually an implementation detail that Apple deliberately doesn't expose, I guess. NSMutableArray is a class cluster which means you don't actually get an instance of NSMutableArray but some other, specialized class following the same interface. And Apple doesn't tell you which class gets returned in which case and how it's behaving. So it's hard to give real advices here.

如果你真的知道,你需要一个容量 X ,只需使用它。否则,除非你有性能问题,我不会在乎所有的容量,只需使用 [NSMutableArray数组] ...

If you really know that on average you'll need a capacity of X, just use it. Otherwise, unless you have performance problems I wouldn't care about the capacity at all and just use [NSMutableArray array]...

这篇关于NSMutableArray initWithCapacity细微差别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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