Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Tests for post: http://chchrn.com/2021/09/19/vdsp_for_wind.html
// CHCHRN
import XCTest
import Accelerate
final class ArrayTransformer {
func floatArray(intValues: [UInt8],
min: Float,
max: Float) -> [Float] {
let step = (max - min) / 256
let result = intValues.map { (v: UInt8) -> Float in
return Float(v) * step + min
}
return result
}
}
final class DSPTransformer {
func floatArray(intValues: [UInt8],
min: Float,
max: Float) -> [Float] {
var step = (max - min) / 256
let count = intValues.count
let stride = vDSP_Stride(1)
let n = vDSP_Length(count)
var fltArray = [Float](repeating: 0, count: count)
vDSP_vfltu8(intValues, stride,
&fltArray, stride,
n)
var mulArray = [Float](repeating: 0, count: count)
vDSP_vsmul(fltArray, stride,
&step,
&mulArray, stride,
n)
var mMin = min
var result = [Float](repeating: 0, count: count)
vDSP_vsadd(mulArray, stride,
&mMin,
&result, stride,
n)
return result
}
}
class WindArrayPerformanceTest: XCTestCase {
private var min: Float!
private var max: Float!
private var uint8Values: [UInt8] = [UInt8]()
override func setUp() {
self.min = 10
self.max = 90
let count = 2000 * 2000
let uint8Range = (0..<UInt8.max)
self.uint8Values = (0..<count).map { _ in uint8Range.randomElement()! }
}
func testSwiftArrayPerformance() {
var floatValues = [Float]()
let transformer = ArrayTransformer()
self.measure {
floatValues = transformer.floatArray(intValues: self.uint8Values,
min: self.min,
max: self.max)
}
print("\(floatValues.count)")
}
func testDSPPerformance() {
var floatValues = [Float]()
let transformer = DSPTransformer()
self.measure {
floatValues = transformer.floatArray(intValues: self.uint8Values,
min: self.min,
max: self.max)
}
print("\(floatValues.count)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment