Swift vs Kotlin performance on sorting array
我尝试在
我记录了一些结果,我发现当大小在10000左右或更小时,Swift更快,但一旦数字增加,与
Swift和Kotlin代码如下,
迅捷
1 2 3 4 5 6 7 8 9 10 | 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 |
科特林
1 2 3 4 5 6 7 8 9 10 | 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 |
两者的记录时间如下,
迅捷
1 2 3 4 5 6 7 8 | 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! |
科特林
1 2 3 4 5 6 7 8 | 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 |
所以,在这里,你可以看到EDCOX1,0,当尺寸增加时变得非常慢,虽然当数字小的时候它更快。
正如martinr强调使用
版本配置为发布的swift
1 2 3 4 5 6 7 8 | 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 |
这可能有助于检查
在调试和发布配置中,编译器使用不同的优化级别,从而影响性能。关于优化级别的一些讨论可以在这个问题中找到。