Created
September 19, 2021 16:11
-
-
Save chchrn/da4e69965f3c667b15c1a2eb7546400d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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