I have a problem. I want to know which one is indeed faster(Swift or Objective-C) because I would like to choose a faster/better one when I start developing an app. According to many sources(For example Apple's WWDC, or http://www.jessesquires.com/apples-to-apples-part-two/), Swift is suppose to be faster.
I just wrote a simple recursive fibonacci sequence program in both Swift and Objective-C.
However, when I run fib(35) on the simulator, I get surprising results:
Objective-C Result:
:::fib::::9227465:::duration:::0.122813 seconds
Swift Result
:::fib::::9227465 :::duration:::0.606831073760986 seconds
Now, I even ran the Swift version in all Swift Compiler Optimization level(for Debug), which is None, Fastest, Fastest-Unchecked. I also play around with the Code Generation Optimization Level to None, Fast....Fastest Aggressive Optimization. However all Swift results are something close to 0.6 milliseconds
Now the last thing I can think of is may be, I am comparing an Apple to Orange? Do you guys see anything I am missing here? Is there anything else I have to turn on(other than Optimization levels for Swfit Compiler & Apple LLVM Code Generation) to make Swift programs run faster?
Any suggestions or comments are welcome and appreciated ! ^^ !
Objective-C Version
-(int)fib:(int)num{
if (num == 0) {
return 0;
}
if (num == 1) {
return 1;
}
return [self fib:num - 1] + [self fib:num - 2];
}
Swift Version
func fib(num: Int) -> Int{
if(num == 0){
return 0;
}
if(num == 1){
return 1;
}
return fib(num - 1) + fib(num - 2);
}
Objective-C Time Measurement
NSTimeInterval start = [[NSDate date] timeIntervalSince1970];
int result = [self fib:35];
NSTimeInterval end = [[NSDate date] timeIntervalSince1970];
NSTimeInterval duration = end - start;
NSLog(@":::fib::::%d:::duration:::%f",result,duration);
Swift Time Measurement
var start = NSDate().timeIntervalSince1970;
let result = fib(35);
var end = NSDate().timeIntervalSince1970;
var duration = end - start;
println(":::fib::::\(result) :::duration:::\(duration)");
Performance. The official Apple website claims that Swift is 2.6 times faster than Objective-C. However some studies indicate that the difference is not as dramatic. Swift and Objective-C are both statistically typed languages that use the same iOS SDK and the high-quality Low Level Virtual Machine compiler.
Swift is easier to read and easier to learn than Objective-C. Objective-C is over thirty years old, and that means it has a more clunky syntax. Swift streamlines code and more closely resembles readable English, similar to languages like C#, C++, JavaScript, Java, and Python.
As Objective-C is harder to learn, there are more new developers learning Swift than learning Objective-C. On the other hand, experienced developers familiar with Objective-C are usually familiar with Swift, or at least ready to learn it.
Lot of things to consider when deciding which of the two programming languages are faster. I did a couple of benchmarks (https://github.com/vsco/swift-benchmarks) between Swift and Objective-C and I found that in certain cases Swift was faster and in other cases Objective-C was faster. For example, using struct objects in Swift will offer tremendous performance gains if you need to operate over a large amount of data. In contrast, using non-struct objects made Swift significantly slower than it's Objective-C counterparts.
Also how you use certain features in Swift is very crucial to how well it will perform. Take this function for example:
class func shuffleGenericObjects<T>(inout array:[T]) {
for (var i = 0; i < array.count; i++) {
let currentObject: T = array[i]
let randomIndex = Int(arc4random()) % array.count
let randomObject: T = array[randomIndex]
array[i] = randomObject;
array[randomIndex] = currentObject
}
}
Sure it works great for minimizing repetitive code, but when I executed this method over 1 million Int objects, it took roughly 32 seconds to finish. As oppose to the non-generic implementation, which only took 0.181 seconds.
I also recommend not to use NSDate
functions for benchmarking in Swift. I came across a few bugs that caused NSDate
to return incorrect times. It's much better to put your benchmarks in an XCTestCase
and use the measureBlock()
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With