On 16 Sep 2017, at 14:52, Quincey Morris <quinceymorris@...> wrote:
On Sep 15, 2017, at 21:18 , Gerriet M. Denkmann <g@...> wrote:
So I think the code looks like this (syntax-checked in Xcode 9 but not tested):
var sortedArray = [UInt32]()
qsort_b (&sortedArray, sortedArray.count, MemoryLayout<UInt32>.stride) {
(aPtr, bPtr) -> Int32 in
guard let a = aPtr?.load (fromByteOffset: 0, as: UInt32.self) else { return 0 } // or otherwise handle a nil pointer
guard let b = bPtr?.load (fromByteOffset: 0, as: UInt32.self) else { return 0 } // or otherwise handle a nil pointer
return a > b ? 1 : a < b ? -1 : 0
}
I tried this in Xcode Version 8.3.3 (8E3004b) (Swift 3) and:
1. it works: thank you very much. I never would have figured this out from the documentation alone.
2. qsort_b is about 2.25 times slower than sort (tested with arrays of size 1 mio to 100 mio)..
I also tried a C version with a malloced sortedArray: about the same speed as Swift with qsort_b.
This is the first time I have seen Swift working swifter than Objective-C (and not by a small margin too!).
I am very impressed with the Swift sort.
It would be interesting to know what kind of optimisation magic Swift does here.
Kind regards,
Gerriet.