在日常开发中,经常会遇到判断一个Array
中元素个数是否为0。一般来说,有两种方法,一种是isEmpty
,一种是array.count == 0
。那哪一种方法更好呢?在苹果的官方文档中,isEmpty
方法上有这样一段注释
/// When you need to check whether your collection is empty, use the `isEmpty` property instead of checking that the `count` property equal to zero.
/// For collections that dont conform `RandomAccessCollection`, accessing the `count` property iterates through the elements of the collection.
- Complexity: O(1)
复制代码
大致意思就是,当判断你的集合是否为空时,推荐使用isEmpty
属性来代替判断count
属性是否等于0。因为集合类型的count
属性会遍历集合里的所有元素。isEmpty
属性的时间复杂度为O(1)。 接下来我们就从源代码角度来分析一下这2者的区别吧。以下的代码位于github
的stdlib/public/core/Collection.swift
文件里。
public protocol Collection: Sequence {
var isEmpty: Bool { get }
var count: Int { get }
var startIndex: Index { get }
var endIndex: Index { get }
}
复制代码
首先,isEmpty
和count
都是Collection
协议的计算型属性。其次,都有一个默认实现。
isEmpty 实现
extension Collection {
public var isEmpty: Bool {
return startIndex == endIndex
}
}
复制代码
isEmpty
方法的实现很简单,判断startIndex
和endIndex
是否相等就可以了。那startIndex
和endIndex
又是2个计算型属性,那么这2个的实现又是怎么样的呢?在这个文件里我们没有找到默认实现,所以我们就前往同层文件夹的Array.swift
文件里去查看一下了。 startIndex
的实现很简单,直接返回了一个0
public var startIndex: Int {
return 0
}
复制代码
endIndex
的相对就稍微复杂一点了
@inlinable
public var endIndex: Int {
@inlinable
get {
return _getCount()
}
}
internal func _getCount() -> Int {
return _buffer.count
}
复制代码
看到这儿,里面的再看下去就太深了(我也看不明白了),姑且当作_buffer
是Array
类型的内部实现吧。
count 实现
public var count: Int {
return distance(from: startIndex, to: endIndex)
}
public func distance(from start: Index, to end: Index) -> Int {
_precondition(start <= end,
"Only BidirectionalCollections can have end come before start")
var start = start
var count = 0
while start != end {
count = count + 1
formIndex(after: &start)
}
return count
}
复制代码
count
方法内部调用了一个distance(from:to:)
的方法,而且在distance
内部有一个while
循环遍历直到start==end
,那么count
的事件复杂度就是o(n)
。
因此2个属性相比,在使用的时候,最好还是使用isEmpty
属性判断Array
元素是否为空。
转载于:https://juejin.im/post/5cf542015188253a2b01cb07