Created
March 11, 2020 21:46
-
-
Save tieleman/a46443582046203a159033eb8ea00541 to your computer and use it in GitHub Desktop.
Multiplication example using vDSP
This file contains hidden or 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
| 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