swift - Swift 4.2 中的快速数组比较

标签 swift swift4.2

我有两个包含 11059200 个元素的整数数组。从下面的例子中,

我正在通过比较 d2 中的元素索引将数组 d1 的值更改为 0

下面程序的计算时间是

Comparing started: 2019-03-02 08:45:56 +0000
Comparing finished: 2019-03-02 08:46:00 +0000

这个过程用了 4 秒是更多的时间。

我想减少时间。有没有可能?谢谢

var d1 = [Int]()
var d2 = [Int]()

let value = 11059200

for _ in 0...value{

    d1.append(Int.random(in: 0...value))
    d2.append(Int.random(in: 0...value))
}

print("Comparing started: \(Date())")

var _ = d1.enumerated().compactMap { (index,value) -> Int in

    return d2[index] == value ? 0 : value
}

print("Comparing finished: \(Date())")

更新:

根据 Alexander 的评论,我正在使用 map 将时间从 2-3 秒减少

var _ = d1.enumerated().map { (index,value) -> Int in
  return d2[index] == value ? 0 : value
}

最佳答案

您可能可以通过使用简单 map 而不是 compactMap 来加快速度。您没有返回可选(某种),因此您无需使用 compactMap。事实上,你的表达 d2[index] == value ? 0 : 值被隐式提升为可选,只是为了 compactMap 然后必须花时间解包它。

此外,您可以通过使用 zip 将 2 个序列迭代在一起来简化代码:

import Foundation

func printTimeElapsedWhenRunningCode(title: String, operation: () -> Void) {
    let startTime = CFAbsoluteTimeGetCurrent()
    operation()
    let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
    print("Time elapsed for \(title): \(timeElapsed) s.")
}

let max = 11059200

let d1 = (0...max).map { _ in Int.random(in: 0...max) }
let d2 = (0...max).map { _ in Int.random(in: 0...max) }

printTimeElapsedWhenRunningCode(title: "Enumerating and indexing, comparing using compactMap (original)") {
    let result = d1.enumerated().compactMap { index, value -> Int in
        return d2[index] == value ? 0 : value
    }

    print(result.count)
}

printTimeElapsedWhenRunningCode(title: "Enumerating and indexing, comparing using map") {
    let result = d1.enumerated().map { index, value -> Int in
        return d2[index] == value ? 0 : value
    }

    print(result.count)
}

// just for a benchmark, don't write codel like this.
printTimeElapsedWhenRunningCode(title: "Manual Indexing") {
    var result = Array<Int>()
    result.reserveCapacity(d1.count)
    for i in d1.indices {
        let (d1Value, d2Value) = (d1[i], d2[i])
        let newValue = d1Value == d2Value ? 0 : d1Value
        result.append(newValue)
    }

    print(result.count)
}

// "Best" from a readibility stand-point
printTimeElapsedWhenRunningCode(title: "Zip") {
    let result = zip(d1, d2).map { d1Value, d2Value in
        return d1Value == d2Value ? 0 : d1Value
    }

    print(result.count)
}

这是在未优化构建中的初步结果。这些绝对没有意义。调试构建的目标是让编译器在尽可能短的时间内生成正确的、可运行的程序,而对性能的关注绝对为 0。这对于快速开发迭代非常有用,但对于基准测试毫无用处。

Time elapsed for Enumerating and indexing, comparing using compactMap (original): 6.206556916236877 s.

Time elapsed for Manual Indexing: 0.3380240201950073 s.

Time elapsed for Zip: 7.123739957809448 s.

Time elapsed for Enumerating and indexing, comparing using map: 5.2529460191726685 s.

当您打开优化(swiftc cli 中的 -O 标志,或作为 Xcode 构建目标中的一个选项)时,您会得到一个完全不同的画面:

Time elapsed for Enumerating and indexing, comparing using compactMap (original): 0.5904990434646606 s.

Time elapsed for Enumerating and indexing, comparing using map: 0.22207605838775635 s.

Time elapsed for Manual Indexing: 0.18644499778747559 s.

Time elapsed for Zip: 0.2339940071105957 s.

我会推荐基于 zip 的方法,因为它的可读性。如果性能绝对至关重要,以至于您确定可以为了微小的速度优势牺牲可读性和可维护性,那么切换到手动索引可能是值得的,但这不太可能是这样的。

关于swift - Swift 4.2 中的快速数组比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54955257/

相关文章:

ios - 错误 “Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” 是什么意思?

ios - 反序列化 SIL 全局 "UIEdgeInsetsZero"时遇到 fatal error

ios - 从解析列中获取值到标签中

ios - 如何在 View 加载后更改 StackView 内容?

iOS、通用链接、Swift。 continueUserActivity 不调用

ios - iOS 应用程序的相机、麦克风和音频/视频存储权限

ios - 如何从我的自定义正在进行的调用 UI 结束 callkit 上的调用 session ?

Swift 按案例名称对枚举进行排序

ios - 如何同时执行多个异步方法并在它们全部完成时获得回调?

ios - Stripe 在 Swift 中创建用户函数