Avatar
@swift-main import Foundation func measure(_ body: () -> Void) { let start = Date.timeIntervalSinceReferenceDate for _ in 0 ..< 10 { body() } let end = Date.timeIntervalSinceReferenceDate print((end - start) / 10) } let n = 1000 func makeNestedArray() -> [[Int]] { var a: [[Int]] = [] for _ in 0 ..< n { a.append((0 ..< n).map { _ in Bool.random() ? 1 : 0}) } return a } do { var a = makeNestedArray() measure { let y = (0 ..< n).randomElement()! for x in 1 ..< n { a[y][x] &+= a[y][x - 1] } } } do { var a = makeNestedArray() measure { let y = (0 ..< n).randomElement()! for x in 1 ..< n { let t = a[y][x - 1] a[y][x] &+= t } } }