斯威夫特array.capacity VS array.count [英] Swift array.capacity vs array.count

查看:142
本文介绍了斯威夫特array.capacity VS array.count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解array.count(元件的阵列中的数量)。的 count是有用的遍历数组的元素的。我有点弄array.capacity的要点

I understand the array.count ( the number of elements in the array ). count is useful to iterate over the array's elements. I sort of get the gist of array.capacity

<一个href=\"https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html\"相对=nofollow> 能力 的重新presents共有多少元素的整数值
  阵列可以在不重新分配存储(只读)。

capacity An integer value that represents how many total elements the array can store without reallocation (read-only).

实验

我一直在玩的游乐场,并注意到一个磁盘阵列的容量为偶数(加2)

I have been playing with the Playground and noticed an array's capacity is an even number ( incremented by 2 )

var arr = [1, 2, 3 , 4, 5, 6, 7]
arr.removeLast() // capacity stays the same after a removal
println(arr.capacity) // 8
println(arr.count)    // 6

var arr = [1, 2, 3 , 4, 5, 6]
arr.removeLast()
println(arr.capacity) // 6
println(arr.count)    // 5

问题

有什么用一个阵列的容量?请举一个具体的例子吗?

What is the use of an array capacity ? Please give a concrete example ?

推荐答案

这是磁盘阵列的容量,尤其是它的 reserveCapacity 方法,可以让你在preallocate空间数组。

An array's capacity—in particular, its reserveCapacity method—lets you preallocate space in the array.

如果您要添加元素的数组,你超过其容量,则该数组必须增加其容量。由于雨燕阵列存储连续的元素在内存中,它必须重新分配其内部存储和(通常)从旧的存储到新的存储复制的所有元素。 (请注意,的NSArray 未记录到连续存储它的元素,但我们可以推断,斯威夫特阵列可能不会根据 withUnsafeMutableBufferPointer <存在/ code>的方法。)

If you're adding elements to an array, and you exceed its capacity, then the array must increase its capacity. Since a Swift array stores its elements contiguously in memory, it must reallocate its internal storage and (usually) copy all of its elements from the old storage to the new storage. (Note that NSArray isn't documented to store its elements contiguously, but we can deduce that Swift Array probably does based on the existence of the withUnsafeMutableBufferPointer method.)

如果您知道的事先您打算多少元素添加到阵列中,你可以使用 reserveCapacity 方法preSET阵列的容量,以便它不需要进行任何的重新分配(和相关联的复制)。

If you know in advance how many elements you intend to add to the array, you can use the reserveCapacity method to preset the array's capacity so that it won't need to perform any reallocations (and associated copying).

我能想到的唯一理由问一个阵列的能力是学习系统是如何工作的,以及调试性能问题。

The only reasons I can think of to ask an array for its capacity are to learn how the system works, and to debug a performance problem.

通常你不必担心保留能力。重新分配很少是一个性能问题。迅速用途(我相信)一种有效的再分配时间表,以便重新分配的数目是在所述阵列的最后计数的对数。例如。如果你一次添加一百万的元素之一,斯威夫特应该超过20-30重新分配执行不动了。

Usually you don't need to worry about reserving capacity. A reallocation is rarely a performance problem. Swift uses (I believe) an efficient reallocation schedule so that the number of reallocations is logarithmic in the final count of the array. E.g. if you add a million elements one at a time, Swift should perform no more than 20-30 reallocations.

但是,如果你知道你的阵列将非常大(如,千兆Mac或几十兆上的iOS设备上),或者如果你是填充在性能敏感code路径阵列(例如罐装音频缓冲,将开始微秒内打),可能要储备能力,避免重新分配。

But if you know your array will be very large (like, gigabytes on a Mac or tens of megabytes on an iOS device), or if you are filling the array in a performance-sensitive code path (e.g. filling an audio buffer that will start playing within microseconds), you may want to reserve capacity and avoid reallocations.

您可能不应该担心预留容量,除非你知道重新分配是一个问题,要么是因为探查表明,他们是一个瓶颈,或者因为你有其他证据(如音频缓冲例如音频故障)。

You should probably not worry about reserving capacity unless you know reallocations are a problem, either because the profiler shows that they're a bottleneck or because you have other evidence (like audio glitches in the audio buffer example).

这篇关于斯威夫特array.capacity VS array.count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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