Skip to content

Instantly share code, notes, and snippets.

@tieleman
Created March 11, 2020 21:46
Show Gist options
  • Select an option

  • Save tieleman/a46443582046203a159033eb8ea00541 to your computer and use it in GitHub Desktop.

Select an option

Save tieleman/a46443582046203a159033eb8ea00541 to your computer and use it in GitHub Desktop.
Multiplication example using vDSP
func multiply(_ array: [Double]) -> [Double] {
// Allocate memory for the output array
let output = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
// Make sure we free up the memory once we're done
defer {
output.deinitialize(count: array.count)
output.deallocate()
}
// Perform the multiplication
vDSP_vmulD(array, 1, array, 1, output, 1, vDSP_Length(array.count))
// Return the result
return Array(UnsafeBufferPointer(start: output, count: array.count))
}
let array = Array(stride(from: 0.0, to: 50000.0, by: 1))
let result = multiply(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment