内容简介:在日常开发中,经常会遇到判断一个大致意思就是,当判断你的集合是否为空时,推荐使用首先,
在日常开发中,经常会遇到判断一个 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
元素是否为空。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- swoole之协程channel元素个数
- 用堆找出最小的 N 个数
- leetcode.69.求一个数的平方根
- 统计两个IP地址之间的IP个数
- 效率比较:几种方法判断一个数是否是素数
- C语言实现三个数从小到大排序/输出
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Basics of Web Design
Terry Felke-Morris / Addison-Wesley / 2013-1-28 / USD 98.40
Basics of Web Design: HTML5 and CSS3, 2e covers the basic concepts that web designers need to develop their skills: * Introductory Internet and Web concepts* Creating web pages with HTML5* Configurin......一起来看看 《Basics of Web Design》 这本书的介绍吧!