superman-1529274_1920

Objective-C vs Swift: A performance analysis

Share on Facebook1Share on LinkedIn0Tweet about this on Twitter0Share on Reddit0

As an Objective-C programmer, I’ve been looking for solid reasons to switch to Swift (by the way I still haven’t). Personally, I really like the simplicity of the Swift’s syntax over Objective-C. But that wasn’t enough for me to be sold. So as Apple claims, it’s 40% faster  than Objective-c, I wanted to run a performance test to see if it’s really that fast. I ran adding, removing, retrieving,and fast enumeration operations on Arrays, Sets, and Dictionaries. Well, the results were not really satisfying, after all Swift is not that fast.

For all the test cases I used XCTestCase’s measureBlock method to measure the running times.


Objective-C
- (void)measureBlock:(void (^)(void))block;


Swift
open func measure(_ block: @escaping () -> Swift.Void)

For all data structures, I used 100K elements and ran each test case for 5 times. (measureBlock returns the average time of 10 runs, so the total runs sum to 50 times).
Below are the averages for each data structure with different operations in both Swift and Objective-C.

Arrays

As you can see below, Swift in overall is faster than Objective-C in Arrays, except for the remove operations. Removes are dramatically slow in Swift comparing to Objective-C.

Dictionaries

In overall, Objective-C is faster than Swift with Dictionaries, except for iteration.

Sets

Just like dictionaries, Swift is faster in iteration but slower in other operations.

Results

I can’t say either language is faster than one another by only comparing 3 different data structures with a few basic operations. But we can get a basic idea of advantages and disadvantages of either languages over each other.

To sum up, Swift is fast but NOT as fast as Apple claims. However, this shouldn’t be an excuse not to switch to Swift. (Note to self: Switch to Swift 🙂)

Share on Facebook1Share on LinkedIn0Tweet about this on Twitter0Share on Reddit0
Tags: , ,
 

Leave a Reply

Your email address will not be published. Required fields are marked *

the coder guy A blog about coding, learning, and really random stuff. (Even cooking)