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
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
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
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:
for _ in 1…100_000 {
}
which, surprisingly, makes an enormous difference in performance.
My results:
(all compiled with -O)
Test 1:
modifications – loop as above
2014-11-18 15:34:44.001 SwiftTest[3886:234817] SWIFT
2014-11-18 15:34:44.015 SwiftTest[3886:234817] Execution took: 0.014229 seconds
2014-11-18 15:34:44.016 SwiftTest[3886:234817] OBJC
2014-11-18 15:34:44.077 SwiftTest[3886:234817] Execution took: 0.061686 seconds
Test 2:
modifications: loop, as above, and array.append() vs += [value]
2014-11-18 15:35:20.658 SwiftTest[3886:234817] SWIFT
2014-11-18 15:35:20.661 SwiftTest[3886:234817] Execution took: 0.003232 seconds
2014-11-18 15:35:20.662 SwiftTest[3886:234817] OBJC
2014-11-18 15:35:20.665 SwiftTest[3886:234817] Execution took: 0.002575 seconds
Test 3:
modifications: loop, as above, redefined Person as struct. Added C++ equivalent with std::vector of struct with std::string, integer properties:
2014-11-18 15:39:36.322 SwiftTest[3931:236052] SWIFT
2014-11-18 15:39:36.327 SwiftTest[3931:236052] Execution took: 0.004996 seconds
2014-11-18 15:39:36.329 SwiftTest[3931:236052] OBJC
2014-11-18 15:39:36.347 SwiftTest[3931:236052] Execution took: 0.017826 seconds
2014-11-18 15:39:36.362 SwiftTest[3931:236052] CPP
2014-11-18 15:39:36.376 SwiftTest[3931:236052] Execution took: 0.013709 seconds