Array的介绍:
When the destination array's element type is a nonclass type that bridges to a Foundation type, bridging from NSArray
to Array
performs a bridging copy of the elements to contiguous storage in O(n) time. For example, bridging from NSArray
to Array<Int>
performs such a copy. No further bridging is required when accessing elements of the Array
instance.
Note:The ContiguousArray
and ArraySlice
types are not bridged;instances of those types always have a contiguous block of memory as their storage.
当目标数组的元素类型是桥接到基础类型的非类类型时,从“NSArray”到“Array”的桥接会执行时间复杂度是O(n)的桥接拷贝,就是将元素拷贝到连续存储区。例如,从“NSArray’”到“Array<Int>”的桥接就执行这样的拷贝。当访问“Array”实例的元素时,不需要进一步的桥接。
注释:“ContiguousArray”和“ArraySlice”类型不能桥接,这些两个类型的实例总有一个连续的内存块作为它们的存储区。
ContiguousArray的介绍:
The ContiguousArray
type is a specialized array that always stores its elements in a contiguous region of memory. This contrasts with Array
, which can store its elements in either a contiguous region of memory or an NSArray
instance if its Element
type is a class or @objc
protocol.
If your array's Element
type is a class or @objc
protocol and you do not need to bridge the array to NSArray
or pass the array to Objective-C APIs, using ContiguousArray
may be more efficient and have more predictable performance than Array
. If the array's Element
type is a struct or enumeration, Array
and ContiguousArray
should have similar efficiency.
“邻接数组”类型是一个 总将元素存储在一个相邻的内存区域 的专用的数组。这与“Array”有所不同,如果“Array”的数组元素类型是类类型(class)或者声明@objc
的协议(@objc protocol)的话,可以将其元素存储在一个相邻的内存区域或“NSArray”实例中。
如果数组的“元素”类型是一个类或声明@objc的协议,并且不需要将数组桥接为“NSArray”或将数组传递给Objto-C API,则使用“ContiguousArray”可能比“Array”更有效且具有更好的性能。如果数组的“元素”类型是结构或枚举,则“Array”和“ContiguousArray”应该具有类似的效率。
总结:
1.数组存储 基本类型、结构体、枚举 时,Array
和ContiguousArray
是差不多的;
2.当存储 class、@objc protocol 时,需要考虑两种情况,如果不用桥接不用传到OC-API上的时候,那么用ContiguousArray
更快点。
3.如果用到了桥接或者声明@objc 传递到OC-API,那么使用Array
更快点。