Vote count:
0
I have a problem. I want to know which one is indeed faster(Swift or Objective-C) because I would like to choose to faster/better one when I start developing an app. According to many sources(For example Apple's WWDC, or http://ift.tt/1paqtIO), 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 milliseconds
Swift Result
:::fib::::9227465 :::duration:::0.606831073760986 milliseconds
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?
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);
}
Swift vs Objective-C Fibonacci Sequence Speed Comparison
Aucun commentaire:
Enregistrer un commentaire