Swift with Speed of the Light
I’m very interested in discovering Swift performance and I wrote few blogs about it. I get really interested in comparing C vs Swift after reading Jesse Squires’s article.
Swift vs C
I wanted to compare the most trivial task in Swift and C — for loop
C
Swift
Results: (in release mode of course)
C: 0.000002 sec
Swift: 0.0928 sec
Wow that was fast in C! Hold on a second. It seems like C does some powerful optimisation and doesn’t do any actual calculation in the loop, but calculates the result at compile time. (I changed the number of the iterations and the result stays the same.)
Let’s make the task a bit harder and generate random numbers.
I changed the loop body to :
sum += arc4random_uniform(UINT32_MAX)
Results:
C: 1.979127 sec
Swift: 1.978747 sec
Swift Speed = C Speed
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
Want to Learn More?
Check out “Swift Hight Performance” book