Reputation: 332
I'm writing several performace benchmarks mainly to compare the performance of cross-platform tools. One of these is a native implementation of bubble sort in Swift.
I've implemented it as folows:
do{
swapped = false
for (var i = 1; i < testSize; i++){
if (sortArray[i-1] > sortArray[i]){
temp = sortArray[i-1]
sortArray[i-1] = sortArray[i]
sortArray[i] = temp
swapped = true
}
}
}while(swapped == true)
Running this for an array of 2500 takes almost 20 seconds on an iphone 4s.
That's hilariously slow. I've implemented the same piece of code using cordova (javascript) and that only takes 2 seconds.
I've also found results of the same test in objective c tested back in 2012 and back then the native time was about 0.6 seconds.
Doing an input validation benchmark also returned comparable results.
Now I'm trying to find out why this is.
Is it because I implemented it incorrectly?
Or is Swift not optimized yet for this type of actions?
Or is it simply because Swift is not optimized for older devices like the iPhone 4s?
Or is it still something else?
Upvotes: 1
Views: 571
Reputation: 14380
Enable all optimizations trough setting the compiler optimizations in build settings:
There are three different options for optimizations:
None [-Onone] no optimizations, the default for debug.
Fastest [-O] perform optimizations, the default for release.
Fastest, Unchecked [-Ounchecked] perform optimizations and disable runtime overflow checks and runtime type checks.
You should pick -O
when performing speed tests, -Ofast
should only be used when you check for every possible array index and overflow.
Upvotes: 1
Reputation: 332
Apparently I made the foolish mistake of setting the wrong optimizations. The optimizations for the LLVM are higher up the list so when I scouted for optimization I unfortunately ended up setting that one.
What I should have done instead was simply lookup "optimization" with the search function, which shows all possible optimizations.
After that it was simply setting the optimization for Swift code generation to Fastest[-O] to gain the performance I was expecting
Upvotes: 0