12

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.

Kamran
  • 14,987
  • 4
  • 33
  • 51
  • Leave comment when you downvote the question. – Paresh P. Apr 24 '18 at 06:36
  • you better know your setup – Wings Apr 24 '18 at 06:40
  • @V_rohit I didn't do any special except creating a project for both Swift and Kotlin by adding the code above. Also i am using the latest xcode, ij, java and mac os. – Kamran Apr 24 '18 at 06:55
  • 1
    How are you running the Swift? If you’re running it in a playground know that it’s interpreted (not very efficiently) and very slow for many things. – David Berry Apr 24 '18 at 06:56
  • @DavidBerry I am running on simulator. I tried in playground also but it was worst so i stopped. These results are not from playground. – Kamran Apr 24 '18 at 06:59
  • 2
    Sorting 100,000,000 elements takes approx 16 seconds on my MacBook Pro when compiled as a Xcode project in Release configuration, i.e. with optimizations. – Martin R Apr 24 '18 at 07:02
  • See also [Swift performance: sorting arrays](https://stackoverflow.com/questions/24101718/swift-performance-sorting-arrays) – Martin R Apr 24 '18 at 07:02
  • @MartinR Looks like a good highlight. This is what i meant for setup. I will try with release mode and update the findings. – Kamran Apr 24 '18 at 07:18
  • 5
    Probably irrelevant here, but note that a Kotlin Int corresponds to `Int32` in Swift. – Martin R Apr 24 '18 at 07:25
  • 1
    So Swift is faster than Kotlin now (and the final question makes no sense anymore). Doesn't that make your question obsolete? – Martin R Apr 24 '18 at 08:01
  • @MartinR yeah. Let me update. – Kamran Apr 24 '18 at 08:04
  • 1
    @Kamran I think the result after your update is reasonable. What is the problem now? – Joshua Apr 24 '18 at 08:06

1 Answers1

14

As MartinR highlighted to compile with release build configuration for Swift so i changed it to release and with that it seems Swift is faster for any number as shown in the below results,

Swift with Build Configuration as release

Size: 1000    Time:  0.001 sec
Size: 10000    Time: 0.001 sec
Size: 100000    Time: 0.006 - 0.007 sec
Size: 1000000    Time: 0.076 - 0.081 sec
Size: 10000000    Time: 0.891 - 0.898 sec
Size: 100000000    Time: 9.01 - 10.14 sec

Size: 1000000000    Time: 113.87 - 117.285 sec

This might be helpful for someone to check Swift results in release build configuration when doing some kind of performance benchmark as shown above, the results vary significantly in debug and release configurations.

In debug and release configurations, compiler uses different optimization levels so it affects the performance. Some discussion on optimization levels can be found in this question

Kamran
  • 14,987
  • 4
  • 33
  • 51