Avatar
@swift-5.6.3 import Foundation let arraySize = 1000000 let loopCount = 1000 class Values { var values = [Int](repeating: 0, count: arraySize) func foo1() { values[0] += values[0] } func foo2() { let v = values[0] values[0] += v } } func measure(_ body: () -> Void) { let time1 = Date.timeIntervalSinceReferenceDate for _ in 0 ..< loopCount { body() } let time2 = Date.timeIntervalSinceReferenceDate print(time2 - time1) } let values = Values() measure { values.foo1() } measure { values.foo2() }