Swift VS Objective-C: Choosing a Faster Language
If compared with Objective-C, which has almost reached its 30-year edge, Swift 1.0 may be considered as a relatively new programming language. On the other hand, it has a huge number of fans among developers despite the fact it is far from being as mature as its predecessor is.
Developers share numerous opinions regarding Swift and share their experience to decide, which language is eventually better. Some choose to opt for a newly introduced language due to its performance aspects. Apple lovers stress such key issues as simplicity and high level of safety. It is clear, that none of the rookies should take any opinion for granted. The best bet is to check, which language is faster. This is the main goal of the given article. Join us for more information and find out the difference between Swift and Objective-C.
Key Factors to Compare
In order to pick a winner, we need to define basic issues necessary to compare. Based on LLVM infrastructure, both come with a single iOS SDK. For this reason, some programmers find it difficult to decide, which the best option is. Both languages work in the same way when it comes to Cocoa frameworks.
The only way to compare both languages is to check their data structures. You can opt for the native solution provided by Swift and the Foundation framework from Objective-C. These frameworks will make it possible to compare the level of performance.
Estimating the level of performance is not as complicated as it may seem at first sight. This rate is based on the period necessary to complete a particular operation. The less time you need for it, the better data structure is. Developers need to take into account a time to execute a particular operation using data. However, some may find it difficult to get the exact data.
Here is what you can do: enable the test structure in order to define it. All you need is to have your structure placed under two operation requests. Do not forget about two-time notches for every operation. You can opt for any basic class like NSdate. It would be perfect for different types of operations based on time. Several types of constructions will let you estimate the time to execute an operation. You can look for some simpler ways to proceed with the measurement.
Speaking of data types, we recommend using String for operations to execute with Swift. NSString would be perfect when testing Objective-C, as these are the objects that are commonly found during the app development process. At the same time, they have enough resources to perform all necessary measurements.
We provide the results obtained from testing using iOS 8.3 that powers iPhone 5. It appeared that Objective-C is at least 10 times faster if compared with Swift. This is due to NSArray implementation, which proves to be faster than Array-Read.
You are also shocked with such disturbing results, aren’t you? However, it appeared that a test was run in the default scheme, which is obviously wrong including XCode debug configurations. It means that the test was rather inaccurate in spite of its speed. Whenever you want to obtain the most precise data when running a test, you need to follow to the Build Setting where you will find an optimization level flag. Now you need to switch between modes and opt for Release. Now you are free to run the test for another time.
Now Objective-C provides the same data with some slight differences. On the other hand, Swift now look much better! It appears that Swift is more dependable that Objective-C. Let’s find out why.
The reason why Swift is so affected is that it has a static checking type in comparison with dynamic nature implemented in Objective-C.
The main feature of Swift is that you can witness all methods in advance. You are not able to add them on the go. Moreover, its classes cannot be created before or after compile-time. In other words, you can benefit from a clear picture in advance and easily optimize the code without facing any difficulties.
At the same time, you will hardly be able to proceed with an efficient optimization of Objective-C. Dynamic languages are tended to operate much slower if compared with static languages.
- As soon as you are done with optimization, you will witness less disturbing results:
- Update, Read and Add operations requires less time for execution in Array when compared with NSArray;
- Swift is obviously faster than Objective-C;
- Objective-C deletes data 10 times faster than Swift due to anomalies observed in “delete by index” box.
Is XCTest Actually a Good Way to Estimate Performance?
It is certainly not! On the one hand, you are very likely to see the results you expect and need. On the other hand, you will certainly come across some unclear obstacles and questions without answers. The most crucial obstacle is an inability to examine the algorithmic complexity hypothesis. There is now way to proceed with that operation. It means that you will never be able to obtain results that describe operation performance dynamics regarding the amount of elements within a separate structure instance.
The last but not the least is a low accuracy of the examination. So, you are very unlikely to come up with a precise performance conclusion. This is due to a few side operations that are launched together with the test. They contract with final results. Is there any other and more efficient approach to proceed with the testing?
Efficient Method to Measure Performance
It is clear that XCTest is hardly the best bet when measuring performance due to a lack of accuracy and flexibility. We recommend excluding this test at all.
You can opt for a more effective approach that will let you benefit from more accuracy and flexibility when measuring performance. We speak of a data structure that is initialized beforehand featuring a predictable number of elements. You may create a new structure with a fixed number of initial states and run the operation once more.
The results were as follows:
- Swift is 4 times faster when it comes to adding the first element;
- Both languages feature constant time when performing an operation;
- Objective-C appeared to be twice faster when it comes to filled Arrays;
- Objective-C fails to perform the same efficiency when deleting elements from Array;
- Swift appears to be 4-6 times faster in general;
- Both languages come with linear complexity;
- Swift provides a clearer constant complexity, while Objective-C offers constant function in the end only;
- Swift sometimes calls for memory allocation in order to add some new elements. Time consumption peaks can be regularly observed during the operation. However, function 2x lets you increase the size of Dictionary.
All these factors may differ depending on the operation. Generally, Objective-C is faster. At the same time, we have enough results in order to come up with a set of crucial conclusions letting you decide, which language is faster and why.
Swift VS Objective-C – Conclusions
After a set of experiments and operations, we can say for sure that Array appears to be at least 2 times faster if compared with NSArray. Do not forget to increase the size of the Dictionary to add a maximum number of elements that you are sure of beforehand. The algorithms used in both data structures has proved to be rather effective although they need some time to execute operations.
Objective-C appeared to be more effective when it comes to handling insert operations within Dictionary and Set. Imagine that you need to handle a huge number of different elements. Swift’s Array looks more preferable, as it proves to be more effective with different types of data for all operations with the exception of Search. Set is also a good solution while sing Dictionary has no sense. Use it in case you do not have anything else to choose from.
So, What Languages is Better?
In spite of all these comparisons, we still have not discussed the coding process. It also contains several essential factors that will let you evaluate the quality of both Swift and Objective-C programming languages. Those factors traditionally include:
- Clarified syntax;
- Efficient compiler;
- Safety and the number of bugs.
- After comparing both languages, you will see the following results.
This language offers lot of great things including managing tools for safe memory, simple rules of inheritance, strong typing and more;
You will find Swift much easier to read if compared with Objective-C. The language is cleaner. You can take the advantage of special modules eliminating class prefixes in addition to numerous filters;
You are free to create new lightweight classes featuring a high level of flexibility. They will contain only necessary elements you need. Everything is simple and clear. Print “Printable” to add a description or “Comparable” to compare;
Although Swift is not the fastest language, it is certainly faster than Objective-C.
- Some errors will make you feel confused and misled;
- IDE support is rather poor;
- XCode (native IDE) is full of bugs.
One would hardly argue Swift has evolved since its debut. No matter if you need to complete some small of medium commercial projects, this language will certainly be the best bet. On the other hand, Objective-C still provides a faster development process.