Swift 4.2中的快速数组比较

问题描述 投票:1回答:1

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

我正在通过比较d1中的元素索引将数组d2的值更改为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())")

更新:

根据亚历山大的评论,我使用地图来减少2-3秒的时间

var _ = d1.enumerated().map { (index,value) -> Int in
  return d2[index] == value ? 0 : value
}
swift swift4.2
1个回答
1
投票

您可以通过使用简单的地图而不是compactMap来加快速度。你没有返回Optional(有点),所以你不需要使用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)
}

以下是未优化构建的初步结果。这些绝对毫无意义。调试版本的目标是让编译器在尽可能短的时间内生成正确的,可运行的程序,并且完全注意性能。它对于快速开发迭代非常有用,但它对于基准测试来说毫无用处。

枚举和索引所用的时间,使用compactMap(original)进行比较:6.206556916236877 s。

手动索引的时间:0.3380240201950073 s。

Zip的时间:7.123739957809448 s。

Enumerating和indexing的时间已过,使用map进行比较:5.2529460191726685 s。

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

枚举和索引的时间已过,使用compactMap(original)进行比较:0.5904990434646606 s。

Enumerating和indexing的时间已过,使用map进行比较:0.22207605838775635 s。

手动索引编制的时间:0.18644499778747559 s。

Zip的时间:0.2339940071105957 s。

我建议使用基于zip的方法来实现其可读性。如果性能绝对至关重要,那么你已经确定可以牺牲可读性和可维护性以获得微小的速度特权,那么切换到手动索引可能是值得的,但这种情况不太可能发生。

© www.soinside.com 2019 - 2024. All rights reserved.