Swift vs Obj-C Performance Comparison
Is Swift faster than Objective-C? This question has been asked so many times and the answer is still unclear. So I took the recent Xcode 7.3 beta and ran some tests comparing Swift 2.2 and Objective-C. The results were surprising even for me.
I found many tests proving that Swift is faster than Objective-C and some saying old Obj-C is swifter than Swift. The truth is in the middle. Straight out in some cases Swift is much faster, but Obj-C still has some advantages.
In tests, I use native data structures for Swift (like Array
, Dictionary
and Set
) and Objective-C (it means NSArray
, NSDictionary
and NSSet
). I also use XCTest
‘s measureBlock
in both tests. It is pretty easy and available for Swift and Obj-C. The tests run with standard Release optimization, because it is well known that Swift is much slower than Obj-C in debug.
The metodology I use is fair. All data structures are filled with one million elements (String
or NSString
with next or randomized numbers) and I run each test 10 times (in fact tests run 10 times in each run by default, so it means they run 100 times).
Arrays
A basic and the most popular data structure in almost all programming languages. Furthermore it is really fast in Swift.
Sets
Unfortunately Set
is not faster than NSSet
. In fact it is two times slower when adding and removing elements.
Access operation means here contains()
function.
Dictionaries
This really useful data structure is similar in both languages. Adding is slower in Swift, but then you can enumerate faster through it.
Conclusion
I would like to say Swift is faster, but I cannot. It is not slower at all, but also is not three times faster like Apple is saying. Although it does not mean we should go back to Obj-C. Swift is more comfortable – lets you do more with less work needed to do. In addition to cool features like strong typing and generics makes it perfect choice.
I am going to run more tests and compare different loops and sets more precisely (like intersect
and union
funcs) in the next few weeks. Looking forward to see you soon!
You can find complete tests source code on Droids on Roids’s GitHub repository.
About the author
Ready to take your business to the next level with a digital product?
We'll be with you every step of the way, from idea to launch and beyond!
Świetna analiza. Od dłuższego czasu sie do tego zbierałem. Będzie świetny materiał do porównania. Mogę ewentualnie zwrócić uwagę na użyte obiekty. NSArray –> NSMutableArray
[…] this isn’t true for all cases. One study exhibits that this difference is not black and white in nature. While Swift 5.1 comes with […]