Objective-C vs Swift – Faceoff 2.0

Not long ago, when Swift was still in its infancy beta stages, I decided to run the two languages through some test side-by-side and see how they compared against one another when it came to speed of execution. You can find the article here. Time has come and Swift is officially released along with iOS 8 and its time to revisit the performance comparison.

The Measurement

We’ll be using the exact same setup that we’ve used in the original comparison for consistency.

The Tests

To avoid duplicating all the test code again, I’ll skip it in this article and instead give you a reference to the original comparison where you can find all the tests. Here we’ll jump straight to the results. Again, I’m using an iPhone 5S (same as last time) for all these test, however, the major difference here is that iOS 8 is also out of beta.

Test 1 – String Manipulation

1

2

3

4

5

6

7

8

9

10

11

/*

* Original Comparison (in beta)

*

* Objective-C: 0.1527 seconds

* Swift: 8.3505 seconds // More than 54 times slower !!

*

* Current Results

*

* Objective-C: 0.1730 seconds

* Swift: 2.2885 seconds // More than 13 times slower !!

*/

Great news! Looks like Swift is seeing some performance improvements coming out of beta. This is a great start, however, it’s still more than 13 times slower than the Objective-C.

Test 2 – Mutable Arrays

1

2

3

4

5

6

7

8

9

10

11

/*

* Original Comparison (in beta)

*

* Objective-C: 0.0061 seconds

* Swift: 5.7418 seconds // More than 941 times slower !!

*

* Current Results

*

* Objective-C: 0.0067 seconds

* Swift: 3.0766 seconds // More than 459 times slower !!

*/

While here we do see an improvement, there’s really no excuse for a simple operation like this to take an astonishing 3 seconds! For those that are curious to try this at home, the Swift syntax has changed slightly for appending elements. The Array object no longer supports appending elements via the array += element syntax. Instead, the append() method is called on the Array object like so array.append(element).

Test 3 – Model Objects

1

2

3

4

5

6

7

8

9

10

11

/*

* Original Comparison (in beta)

*

* Objective-C: 0.0427 seconds

* Swift: 8.4627 seconds // More than 198 times slower !!

*

* Current Results

*

* Objective-C: 0.0492 seconds

* Swift: 3.1295 seconds // More than 63 times slower !!

*/

This is a big improvement for Swift. While still deathly slow for simple model object creation, we do see that Swift shaved off more than 5 seconds on that operation!

Conclusion

Swift has finally left the beta stages and we are seeing some major advancements in performance, however, there is still a lot of room for improvement. I think the real takeaway here is that Swift will continue to mature and we can expect to see further refinement in the years to come. The language is still very young and it has a very exciting future ahead of it, however, in the mean time I will be sticking to the good old Objective-C with ALL the baggage of C, which I don’t think I could live without.

Interesting comparison… I’ve tried the same with a few modifications and found that Swift can be sped up to a speed exceeding ObjC with some changes. For example, the for loop needlessly assigns otherwise unused i a value. In Swift you can replace the loop declaration like so: