Category: Blog, iOS, Development

Swift vs Obj-C Performance Comparison

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.

Arrays in Swift and Objective-C

Sets

Unfortunately Set is not faster than NSSet. In fact it is two times slower when adding and removing elements.

sets

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.

dictionaries

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!

About the author

Piotr Sochalewski

Piotr Sochalewski

Droids On Roids iOS Developer