【发布时间】:2017-03-11 13:25:27
【问题描述】:
我正在尝试加快我当前对将 [UInt32] 转换为 [UInt8] 的函数的实现,该函数又被拆分为 [[UInt8]],每个索引处有 6 个数组。
我的实现:
extension Array {
func splitBy(subSize: Int) -> [[Element]] {
return 0.stride(to: self.count, by: subSize).map { startIndex in
let endIndex = startIndex.advancedBy(subSize, limit: self.count)
return Array(self[startIndex ..< endIndex])
}
}
}
func convertWordToBytes(fullW : [UInt32]) -> [[UInt8]] {
var combined8 = [UInt8]()
//Convert 17 [UInt32] to 68 [UInt8]
for i in 0...16{
_ = 24.stride(through: 0, by: -8).map {
combined8.append(UInt8(truncatingBitPattern: fullW[i] >> UInt32($0)))
}
}
//Split [UInt8] to [[UInt8]] with 6 values at each index.
let combined48 = combined8.splitBy(6)
return combined48
}
这个函数在我的程序中会被迭代数百万次,它的速度是一个巨大的负担。
有人有什么想法吗? 谢谢
【问题讨论】:
-
您可能更愿意在codereview.stackexchange.com发布此内容
-
您的代码在 Swift 2 中。您想将其保持为 Swift 2 还是同时更新到 Swift 3?
-
这台电脑太旧了,所以现在需要保留在 Swift 2 中。
-
在加速框架中使用向量数学/simd 库,您可能能够钩入损坏但可用的 opencl 实现。这个内核会很小,并且在管道中很容易粉碎 uint8s 比 cpu 线程快得多
标签: arrays swift cryptography uint32 uint8array