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.
- (void)measureBlock:(void (^)(void))block;
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.
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.
In overall, Objective-C is faster than Swift with Dictionaries, except for iteration.
Just like dictionaries, Swift is faster in iteration but slower in other operations.
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 🙂)