1

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?

5
  • 3
    it's because bubble sort itself is ridiculously slow. Commented May 17, 2015 at 21:34
  • @TheParamagneticCroissant Then why can i run the same bubble sort in javascript on the same device 10 times faster? Commented May 17, 2015 at 21:35
  • 2
    did you compile the Swift code with all optimizations enabled? JavaScript engines optimize aggressively these days. Commented May 17, 2015 at 21:36
  • @TheParamagneticCroissant all optimizations? Are there mutliple? I've set the optimization level in code generation to max and it didn't change anything Commented May 17, 2015 at 21:45
  • woops, changed the llvm optimization instead of swifts, results are as expected now around 200ms. It didn't change anything for the input validation though. I'll have to look into that a bit more Commented May 17, 2015 at 22:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

It's what we get all the time - people complain that Swift is slow, and then they figure out that they didn't use the compiler properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.