I sometimes come to a place where I will not change the contents of the array, but I need to know its score several times over the function. Is it more efficient to assign an array value to a variable and use it several times, or does the compiler make an efficiency equivalent?
Let them explore! Is it the myArray.countequivalent of accessing a stored property, or is it a computable property that performs some "unnecessary" calculations if it is called repeatedly for an un mutated array? (Excluding compiler skills)
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!
Thus, at the end it countis a stored property and is not calculated for each call, so there should be no reason to explicitly store the property arr.countyourself.
count
arr.count
struct _SwiftArrayBodyStorage { __swift_intptr_t count; __swift_uintptr_t _capacityAndFlags; };
, Swift. , , . ,
: 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 } }
; , . - , . , Array.count / .
Array.count
Array.count is a pre-computed value. Since it does not compute it on the fly, it is much harder to use it than using memory to save it a second time. However, none of the methods should matter unless you make it millions.
Source: https://habr.com/ru/post/1652375/More articles:Node js and jQuery / Ajax (Like / Unlike) - javascriptHow to enable scala linter? - scalahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1652372/webpack-bundle-copy-all-assets-to-build-or-dist-folder&usg=ALkJrhgi2kew4rCudZCp4Xu3_kSpNOgxKgПредотвращение маршрутизации routerLink Изменение маршрутов с помощью Click Event Handler - angularGoogle Login: Unable to disable iOS app - iosdocker-proxy using port when containers are not running - dockerHaskell Polymorphism with Types and Type Variables - polymorphismExecute a function in the context of another cfc in ColdFusion, following import instructions - jsonWhat is AWS Public IP Limit? (Public IP inelastic IP) - amazon-web-servicesCan I configure to test Android on multiple modules? - android-studioAll Articles