在Swift中调用Array上的.count分配count变量

时间:2016-08-24 17:57:37

标签: arrays swift count compiler-optimization enumeration

我偶尔来到一个我不会改变数组内容的地方,但我需要在函数上多次知道它的计数。将数组的.count分配给变量并多次使用它是否更有效,或者编译器是否使效率等效?

4 个答案:

答案 0 :(得分:3)

让我们调查一下! myArray.count是否等同于访问存储的属性,或者它是一个计算属性,执行一些"不必要的"如果为非变异数组重复调用计算? (无视编译器的聪明)

/// The number of elements in the array.
public var count: Int {
  return _getCount()
}

// ... what is function _getCount()?

internal func _getCount() -> Int {
  return _buffer.count
}

// ... what is property _buffer?
internal var _buffer: _Buffer

// ... what is type _Buffer? (Swift)
internal typealias _Buffer = _ContiguousArrayBuffer<Element>

// ... what is type _ContiguousArrayBuffer?
// --> switch source file
import SwiftShims

/// Class used whose sole instance is used as storage for empty
/// arrays.  The instance is defined in the runtime and statically
/// initialized.  See stdlib/runtime/GlobalObjects.cpp for details.
internal struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {

  // ... conformance to _ArrayBufferProtocol

  /// The number of elements the buffer stores.
  internal var count: Int {
    get {
      return __bufferPointer.header.count
    }
    // ...
  }   
  // ...
}

// ... what is property __bufferPointer?
var __bufferPointer: ManagedBufferPointer<_ArrayBody, Element>

// what is type _ArrayBody?
// we notice for now that it is used in the following class:
internal final class _EmptyArrayStorage
  : _ContiguousArrayStorageBase {
  // ...

  var countAndCapacity: _ArrayBody // telling name for a tuple? :)
}

// --> proceed to core/ArrayBody.swift
import SwiftShims

// ...

internal struct _ArrayBody {
  var _storage: _SwiftArrayBodyStorage

  // ...

  /// The number of elements stored in this Array.
  var count: Int {
    get {
      return _assumeNonNegative(_storage.count)
    }
    set(newCount) {
      _storage.count = newCount
    }
  } 
}

// we are near our price! we need to look closer at  _SwiftArrayBodyStorage, 
// the type of _storage, so lets look at SwiftShims, GlobalObjects.cpp
// (as mentioned in source comments above), specifically
// --> switch source file
struct _SwiftArrayBodyStorage {
  __swift_intptr_t count;              
  __swift_uintptr_t _capacityAndFlags;
};

// Yay, we found a stored property!

所以最后count是一个存储属性,并不是每次调用计算的,所以没有理由自己显式存储arr.count属性。

答案 1 :(得分:2)

struct _SwiftArrayBodyStorage {
    __swift_intptr_t count;
    __swift_uintptr_t _capacityAndFlags;
};

这是Swift实现的结构。根据这个计数,我们总是知道缓冲区中有多少元素。你可能可以使用那个

info form:https://ankit.im/swift/2016/01/08/exploring-swift-array-implementation/

修改更多信息

public var count: Int {
  get {
    return __bufferPointer.value.count
  }
  nonmutating set {
     _sanityCheck(newValue >= 0)

     _sanityCheck(
        newValue <= capacity,
        "Can't grow an array buffer past its capacity")

        __bufferPointer._valuePointer.memory.count = newValue
    }
}

答案 2 :(得分:1)

没关系;我建议你做任何使你的代码更简单,更容易理解的事情。在发布版本中,优化器应该内联并注意到调用之间的值将是相同的。无论如何,.handle(Files.outboundGateway()) 在性能/代码中基本上等同于访问局部变量。

答案 3 :(得分:0)

Array.count是预先计算的值。由于它不会在运行中计算它,因此使用它比使用内存第二次存储它要少得多。即便如此,除非完成数百万+ +,否则这两种方法都不重要。