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
Set) and Objective-C (it means
NSSet). I also use
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 (
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).
A basic and the most popular data structure in almost all programming languages. Furthermore it is really fast in Swift.
Set is not faster than
NSSet. In fact it is two times slower when adding and removing elements.
Access operation means here
This really useful data structure is similar in both languages. Adding is slower in Swift, but then you can enumerate faster through it.
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
union funcs) in the next few weeks. Looking forward to see you soon!