Swift Array 元素个数判断为0的方法哪一种更好

在平常开发中,常常会遇到判断一个Array中元素个数是否为0。通常来讲,有两种方法,一种是isEmpty,一种是array.count == 0。那哪种方法更好呢?在苹果的官方文档中,isEmpty方法上有这样一段注释git

/// 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者的区别吧。如下的代码位于githubstdlib/public/core/Collection.swift文件里。github

public protocol Collection: Sequence {
	var isEmpty: Bool { get }
	var count: Int { get }
	
	var startIndex: Index { get }
	var endIndex: Index { get }
}
复制代码

首先,isEmptycount都是Collection协议的计算型属性。其次,都有一个默认实现。swift

isEmpty 实现

extension Collection {
	public var isEmpty: Bool {
   		return startIndex == endIndex
  	}
}
复制代码

isEmpty方法的实现很简单,判断startIndexendIndex是否相等就能够了。那startIndexendIndex又是2个计算型属性,那么这2个的实现又是怎么样的呢?在这个文件里咱们没有找到默认实现,因此咱们就前往同层文件夹的Array.swift文件里去查看一下了。 startIndex的实现很简单,直接返回了一个0bash

public var startIndex: Int {
    return 0
  }
复制代码

endIndex的相对就稍微复杂一点了dom

@inlinable
public var endIndex: Int {
	@inlinable
    get {
      	return _getCount()
    }
 }
  
internal func _getCount() -> Int {
	return _buffer.count
}
复制代码

看到这儿,里面的再看下去就太深了(我也看不明白了),姑且看成_bufferArray类型的内部实现吧。ui

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

所以2个属性相比,在使用的时候,最好仍是使用isEmpty属性判断Array元素是否为空。code

相关文章
相关标签/搜索