{array|dictionary}WithCapacity 应该如何使用? [英] How should {array|dictionary}WithCapacity be used?

查看:49
本文介绍了{array|dictionary}WithCapacity 应该如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 NSMutableArrayNSMutableDictionary 时,如果我知道要放入的元素数或最大元素数,我通常使用 arrayWithCapacitydictionaryWithCapacity,但我想知道为数组/字典指定(初始)容量真的有帮助吗?

When I use NSMutableArray or NSMutableDictionary, if I know the number of elements I want to put in or the max number of elements, I usually create them with arrayWithCapacity or dictionaryWithCapacity, but I wonder does it really help to specify an (initial) capacity for the array/dictionary?

我不知道它内部是如何实现的,但我相信有可能当一个集合中的元素数量达到容量甚至接近容量时,集合可能会扩展它的容量,所以如果我创建了一个容量为32的可变数组,只要我把第32个对象放进去,它就会扩展到另一个容量?或者即使我把第30个物体放进去,它的容量也会因为它认为会有更多物体而扩大?

I don't know how it is implemented internally, but I believe it is possible that when the number of elements in a collection hits the capacity or even approaches the capacity, the collection may extend its capacity, so if I created a mutable array with capacity 32, as long as I put the 32nd object in it, it will expand itself to another capacity? Or even if I put the 30st object in it, its capacity will be expanded as it thinks there will be more objects?

所以如果这些方法真的有帮助,我应该使用类似的方法:

So if these methods really help, should I use something like:

 *withCapacity:maxNumberOfElements * 1.5

代替

*withCapacity:maxNumberOfElements

所以它对我的对象有足够的容量,并且当我放入所有对象时不会扩展?

so it will have more than enough capacity for my objects and won't expand when I put in all the objects?

推荐答案

当您计划用大量元素填充集合时,或者当您知道要加载的元素的确切数量时,这很有用.调整集合大小会占用 CPU 周期,因此不必要地调整大小最终会减少您的设备使用电池的时间.

This is useful when you are planning to populate a collection with a large number of elements, or when you know the exact number of elements that you are going to load. Resizing a collection takes CPU cycles, so resizing unnecessarily ultimately translates into reducing the time your device can operate on a battery.

考虑这个例子:假设您准备将 3000 个元素加载到一个数组中.如果您为默认数组分配空间,例如 16 个项目,则该数组需要调整大小八次才能达到容纳 3000 个元素所需的大小.每次调整数组大小时都需要复制在初始位置复制的元素,从而导致额外的 3000 次复制操作.当您知道元素的确切数量时,就可以防止发生复制.

Consider this example: let's say you are about to load 3000 elements into an array. If you allocate the default array with space for, say, 16 items, the array will need to resize eight times before arriving at the size necessary to hold 3000 elements. The elements copied in the initial positions will need to be copied each time the array is resized, resulting in 3000+ additional copy operations. When you know the exact number of elements, you can prevent the copying from happening.

此外,您的数组不会为您不打算添加的元素浪费内存:如果您将 3000 个元素一个一个地添加,数组可能会在内部增长到 4000 个,以期待更多元素;最后 1000 个元素将被浪费.

In addition, your array will not waste memory for elements that you are not going to add: if you add 3000 elements one by one, the array may grow to 4000 internally in anticipation of more elements; the last 1000 elements will be wasted.

总而言之,当您确定确切的目标大小时,您应该使用容量初始化您的集合.当您从文件或网络连接反序列化数据时,经常会出现这种情况.在您不知道大小的情况下,最好不要猜测,让默认初始化自行处理.

To summarize, you should initialize your collections with capacity when you know the exact target size for sure. This situation comes up often when you deserialize data from a file or a network connection. In situations when you do not know the size, it is better not to make guesses, and let the default initialization run its course.

这篇关于{array|dictionary}WithCapacity 应该如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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