Swift array.capacity 与 array.count [英] Swift array.capacity vs array.count

查看:45
本文介绍了Swift array.capacity 与 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

容量 一个整数值,表示总共有多少元素数组无需重新分配即可存储(只读).

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

实验

我一直在玩 Playground 并注意到数组的容量是偶数(增加 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 方法——让你预先分配数组中的空间.

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

如果您向数组添加元素,并且超出了其容量,则该数组必须增加其容量.由于 Swift 数组将其元素连续存储在内存中,因此它必须重新分配其内部存储并(通常)将其所有元素从旧存储复制到新存储.(请注意,NSArray 没有记录为连续存储其元素,但我们可以根据 withUnsafeMutableBufferPointer 方法的存在推断出 Swift Array 可能会这样做.)

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方法来预设数组的容量,这样它就不会需要执行任何重新分配(和相关的复制).

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.

通常您无需担心预留容量.重新分配很少是性能问题.Swift 使用(我相信)有效的重新分配计划,以便重新分配的数量在数组的最终计数中是对数的.例如.如果一次添加一百万个元素,Swift 应该执行不超过 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 设备上的几十兆字节),或者如果您在对性能敏感的代码路径中填充数组(例如填充音频缓冲区)将在几微秒内开始播放),您可能希望保留容量并避免重新分配.

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).

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

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