Is Swift Faster Than Objective-C? Swift 3 and Swift 5Enter content title here...

4.4
103
Is Swift Faster Than Objective-C? Swift 3 and Swift 5Enter content title here...

I use 2 different approaches to compare the performance of Swift 3, Swift 5, and Objective-C. Discover the conclusions made after the comparison.

There are a lot of opinions about Swift. Some accuse it of performance issues. Others, like Apple, count performance, simplicity, and safety among Swift’s merits. This programming language has been on the stage for quite a while, and a lot of developers are already using the fifth version. Many say that Swift 5 is much better than previous versions, including Swift 3, and definitely better than Objective-C.

We don’t take anybody’s words for granted, so we decided to check for ourselves whether Swift 5 is better than Objective-C and Swift 3 in terms of performance. If you’re thinking about which to choose for your app development, Swift or Objective-C, check out our test results below to help you make an informed decision in choosing a faster languages and weighing all pros & cons.

What did we compare?

Swift and Objective-C can both be used for iOS app development. The Swift and Objective-C compilers are both based on the LLVM compiler infrastructure, and there’s a single iOS SDK for them. That’s why there isn’t much difference between the ways these programming languages work with Cocoa frameworks.

We decided to measure the performance of Swift 3, Swift 5, and Objective-C by comparing their data structures. For that, we used the Objective-C Foundation framework and Swift’s native solutions.

We wrote several tests to estimate the performance of different types of data structures – Array/NSArray, Dictionary/NSDictionary, and Set/NSSet – by defining write, read, delete, and find operations for each. For that, we used XCode 8.3.3 with Swift 3 and Xcode 10.2.1 with Swift 5 and Objective-C.

How we estimated performance of the data structures

The most common way to measure performance is to identify how long a data structure takes to complete a given operation. To find that out, we considered the execution time of operations with data. But how can we measure that exactly? Here’s what we did:

  • We started by defining the test structure and placing operation requests in the structure with two time notches. We started with NSDate, since it’s a basic class for any time-based operations. There are a few ways to measure execution time with NSDate. One is by using the following construction:
Is Swift Faster Than Objective-C? Swift 3 and Swift 5Enter content title here...

Of course, we could have substituted [NSDate new] with CACurrentMediaTime() for more precise results and wrapped this construction into the method with a block argument in which we would have sent the operation request.

But there’s definitely a more straightforward way to measure the execution time!

  • This solution is described in detail in an old-but-gold article about benchmarking. The C function dispatch_benchmark from the libdispatch library does everything for you. Unfortunately, it doesn’t work with Swift, as we can’t use Swift to call a private C-language API.

We needed something to test both languages, and we found it.

Discover a full article: https://yalantis.com/blog/is-swift-faster-than-objective-c/