Swift too slow without optimization

Hello,
I’m new here, and I wanted to try Swift language to solve some puzzles. However, I’m stuck in a hard puzzle by a timeout, and that reminds me some topics I read on the language :



The fact is that Swift is really slow without compilation optimizations. On some cases, even Ruby could do better!
With that said, I would like to know if compilation optimization could be activated, because the Swift is quite unusable at this time.

3 Likes

For solo puzzles, we’ve allowed much more time for Swift than we do for C or Java. Adding the compilation optimizations has some side effects but I may have an idea, I’ll ask to a colleague on Monday to get his point of view.

2 Likes

You only need to do fifteen puzzles in Swift to get the achievement for “doing a lot of languages in Swift”–so it is understandable if those are to be skipped.

Not saying this is the case here, but timeouts are usually triggered by a not so efficient algorithm being used rather than lack of optimization at the compiler level.

Test cases are written this way: one test case can pass with a given algorithm then the next test case will not. The difference in complexity needed to pass this new test case is usually large enough (e.g. O(n) vs O(n3)) for the compilation optimization to not make a significant difference.

2 Likes

I’ve given up trying to explain that to people in the chat… they never seem to get it…

Maybe the forums will have more luck.

I understand that, but have you read my links ?
Swift can be 100 times more slow without optimization — particularly for nested loops … complex or not, it’s enormous; whereas with optimization, it performs at C level.
I don’t know the tests and testing mechanics concerning timeout for each language, but a 100x factor is maybe not insignificant.
As it is said, are all the puzzles realizable in Python ?

Edit:
Ok, I tried a hard puzzle in Python, and I solved it completely with the 3rd version of my algorithm.
To try, I wrote the 2nd version in C, and it did not pass the last test because of timeout.
I’m now impressed by the quality of the tests.
This post can be interpreted as a newbie’s error.

XorMode is right, the test cases are designed to evaluate the algorithms and not the speed of the implementation. A 100x factor might looks enormous but if n = 10 000, a solution that is O(n^2) will do 100 000 000 iteration while an O(n log n) solution will only require approximatively 130 000 iterations. That’s a 750x factor!

1 Like

… avoid nested loops if at all possible…