Correct way to call realloc in Swift with a Float array?
I've discussed this on Apple's Developer Forum. It turns out that using Swift's alloc allots space for a Swift array, not a C array. So if you want to use MutableUnsafePointer for a C array and "realloc" (bridged from C) you need to stick with C functions, like "malloc" (bridged from C).
By adding the following function, and using it when I initially set up my Floats array, the "realloc" bug went away:
func floats_alloc( qty_of_floats:Int ) -> Floats {
// return Floats.alloc( qty_of_floats )
let qty_of_bytes = ( qty_of_floats * FLOAT_SIZE )
let alloced_floats = Floats(
malloc( UInt( qty_of_bytes ) )
)
return alloced_floats
}
I've tested for a couple weeks now, and all is well.
UnsafeMutablePointerFloat returned by vDSP_fft_zip being overwritten immediately
Your way of creating DSPSplitComplex
is wrong.
When you pass Array<Float>
to UnsafeMutablePointer<Float>
using &
, the address passed to the function is only valid within the function.
So, in your code, two addresses passed DSPSplitComplex(realp: &A, imagp: &B)
are not valid when the initializer of DSPSplitComplex
finished.
To avoid this sort of behavior, you can try something like this:
var A = [Float](repeating:0, count:32)
var B = [Float](repeating:0, count:32)
A.withUnsafeMutableBufferPointer {aumbp in
B.withUnsafeMutableBufferPointer {bumbp in
var splitComplex1 = DSPSplitComplex(realp: aumbp.baseAddress!, imagp: bumbp.baseAddress!)
//--Perform fft
let log2Size = vDSP_Length(log2f(Float(32)))
let setupFFT = vDSP_create_fftsetup(log2Size, FFTRadix(FFT_RADIX2))!
vDSP_fft_zip(setupFFT, &splitComplex1, 1, log2Size, FFTDirection(FFT_FORWARD))
}
}
Or else, you can allocate UnsafeMutableBufferPointer<Float>
s and use them instead of Array<Float>
.
xy array in Swift performance
I checked the performance for various solutions to your problem. You can download my tests from this link to github
A) Your code
var spectrum: [XYPoint] = []
for i in 0..<xArray.count {
let xy = XYPoint(x: xArray[i], y: yArray[i])
spectrum.append(xy)
}
B) Zip + map (Martin R's answer)
let spectrumB = zip(xArray, yArray).map(XYPoint.init)
C) Range + map (My solution)
let spectrum = (0 ..< xArray.count).map { i in
return XYPoint(x: xArray[i], y: yArray[i])
}
D) ReserveCapacity + append (Duncan C's answer)
var spectrum: [XYPoint] = []
spectrum.reserveCapacity(xArray.count)
for (index, value) in xArray.enumerated() {
spectrum.append(XYPoint(x: xArray[index], y: yArray[index]))
}
My results (in seconds)
╭──────────────┬──────────────┬──────────────┬──────────────╮
│ A │ B │ C │ D │
╭───────────╬══════════════╪══════════════╪══════════════╪══════════════╡
│ 100 ║ 0.000009426 │ 0.000002401 │ 0.000000571 │ 0.000000550 │
│ 200 ║ 0.000003356 │ 0.000002629 │ 0.000000911 │ 0.000000866 │
│ 500 ║ 0.000005610 │ 0.000007288 │ 0.000002236 │ 0.000002012 │
│ 1000 ║ 0.000010638 │ 0.000009181 │ 0.000003905 │ 0.000005030 │
│ 2000 ║ 0.000019377 │ 0.000013316 │ 0.000007116 │ 0.000008732 │
│ 5000 ║ 0.000023430 │ 0.000019304 │ 0.000019809 │ 0.000019092 │
│ 10000 ║ 0.000050463 │ 0.000031669 │ 0.000035121 │ 0.000035420 │
│ 20000 ║ 0.000087040 │ 0.000058664 │ 0.000069300 │ 0.000069456 │
│ 50000 ║ 0.000272357 │ 0.000204213 │ 0.000176962 │ 0.000192996 │
│ 100000 ║ 0.000721436 │ 0.000459551 │ 0.000415024 │ 0.000437604 │
│ 200000 ║ 0.001114534 │ 0.000924621 │ 0.000816374 │ 0.000896202 │
│ 500000 ║ 0.002576687 │ 0.002094998 │ 0.001860833 │ 0.002060462 │
│ 1000000 ║ 0.007063596 │ 0.005924892 │ 0.004319181 │ 0.004869024 │
│ 2000000 ║ 0.014474969 │ 0.013594134 │ 0.008568550 │ 0.009388957 │
│ 5000000 ║ 0.038348767 │ 0.035136008 │ 0.021276415 │ 0.023855382 │
│ 10000000 ║ 0.081750925 │ 0.078742713 │ 0.043578664 │ 0.047700495 │
│ 20000000 ║ 0.202616669 │ 0.199960563 │ 0.148141266 │ 0.145360923 │
│ 50000000 ║ 0.567078563 │ 0.552158644 │ 0.370327555 │ 0.397115294 │
│ 100000000 ║ 1.136993625 │ 1.101725386 │ 0.713406642 │ 0.740150322 │
└───────────╨──────────────┴──────────────┴──────────────┴──────────────┘
Related Topics
Swift String Permutations Allowing the Same Strings
How to Animate Changes to an @Observedobject
Alamofire 5 Upload Encodingcompletion
App Crashes from IPA File But Runs Fine from Xcode
How to Create a Multiline Textfield in Swiftui? Like the Notes App
Using Xcode to Cross-Compile Swift to Linux
Swiftui on MACos, How to Use Custom Image Symbol on Button
Swift Probability of Random Number Being Selected
Swiftui: Two-Finger Swipe ( Scroll ) Gesture
Updating Fetchedresultscontroller for Predicate Set by Uisearchbar
How to Understand What Is Causing a Crash Involving '_Nstouchbarfinderobservation'
Nsdateformatter Detect 24-Hour Clock in Os X and iOS
Formsheet iOS 8 Constraints Are Same as Iphones Constraints
How to Subclass a Class Which Doesn't Have Any Designated Initializers
Ibdesignable and Uitableviewcell
Gcd Pattern for Chaining Async Operations While Piping the Results