I tried sorting an array of size 100000000 in Swift
and Kotlin
and i can see a huge performance gap between them. For this number, Kotlin
is almost 18
times faster than Swift
(on my machine).
I recorded some results and i found that swift is faster when the size is around 10000 or less but once the number goes up, Swift
becomes significantly slow as compare to Kotlin
.
Code for Swift and Kotlin is below,
Swift
let n = 100000000
var arr = Array(repeating: 0, count: n)
for i in 1...n {
arr[i-1] = Int(arc4random_uniform(UInt32(n)))
}
//Record time before sort
arr.sort()
//Record time after sort
Kotlin
val n = 100000000
val arr = IntArray(n)
for (i in 1..n) {
arr[i-1] = Random().nextInt(n)
}
//Record time before sort
arr.sort()
//Record time after sort
Time recorded for both is below,
Swift
Size: 1000 Time: 0.001 sec
Size: 10000 Time: 0.009 - 0.01 sec
Size: 100000 Time: 0.122 - 0.127 sec
Size: 1000000 Time: 1.392 - 1.409 sec
Size: 10000000 Time: 16.115 - 16.569 sec
Size: 100000000 Time: 187.346 - 187.71 sec
Size: 1000000000 Waited more than 6 minutes and gave up!
Kotlin
Size: 1000 Time: 0.06 sec
Size: 10000 Time: 0.063 - 0.084 sec
Size: 100000 Time: 0.083 - 0.105 sec
Size: 1000000 Time: 0.23 - 0.501 sec
Size: 10000000 Time: 1.098 - 1.807 sec
Size: 100000000 Time: 10.759 - 11.141 sec
Size: 1000000000 Time: 124.252 - 127.54 sec
So, here you can see Swift
is becoming extremely slow when size increases although it is faster when the number is small.