swift性能问题:本地数组vs全局数组

时间:2022-12-04 21:19:45

I found a strange performance issue when playing with array in swift.

我发现了一个奇怪的性能问题,当玩阵列在斯威夫特。

In the following two demo codes, I try to do a random copy for an array. The only difference between these two codes is the position of the array definition.

在下面两个演示代码中,我尝试为一个数组做一个随机拷贝。这两个代码之间的唯一区别是数组定义的位置。

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        var array: [Int] = [] /* HERE */
        var n: Int = 10000
        var timer = NSDate()
        for i in 0 ..< n {
            array.append(i)
        }
        println("append \(n) elements \(NSDate().timeIntervalSinceDate(timer))")
        for i in 0 ..< n {
            var p: Int = Int(arc4random_uniform(UInt32(array.count - i))) + i
            array[i] = array[p]
        }
        println("permutation \(array.count) elements \(NSDate().timeIntervalSinceDate(timer))")
    }
}
// append 10000 elements 0.0597179532051086
// permutation 10000 elements 0.108937978744507

But when I move the definition of array outside the function (inside the class), then some strange things happened.

但是当我把数组的定义移到函数外(类内)时,就会发生一些奇怪的事情。

import UIKit
class ViewController: UIViewController {
    var array: [Int] = [] /* HERE */
    override func viewDidLoad() {
        super.viewDidLoad()

        var n: Int = 10000
        var timer = NSDate()
        for i in 0 ..< n {
            array.append(i)
        }
        println("append \(n) elements \(NSDate().timeIntervalSinceDate(timer))")
        for i in 0 ..< n {
            var p: Int = Int(arc4random_uniform(UInt32(array.count - i))) + i
            array[i] = array[p]
        }
        println("permutation \(array.count) elements \(NSDate().timeIntervalSinceDate(timer))")
    }
}
// append 10000 elements 0.0645599961280823
// permutation 10000 elements 4.61092203855515

I am a newbie to swift. But I guess I am familiar with other programming language such C++, Java, Python. And this behaviour is REALLY REALLY strange for me. Am I miss anything?

我是斯威夫特的新手。但是我想我很熟悉其他的编程语言,比如c++, Java, Python。这种行为对我来说真的很奇怪。我错过什么吗?

Thanks so much for your help. :>

非常感谢你的帮助。:>

BTW, my XCode version is 6.1 6A1052D, and the simulator is iPad. I use Debug mode to test my code. But even in Release mode, the second code is still much slower than the first one. So Sad. ;<

顺便说一下,我的XCode版本是6.1 6A1052D,模拟器是iPad。我使用调试模式来测试代码。但是即使在发布模式下,第二段代码仍然比第一段慢很多。很伤心。,<

1 个解决方案

#1


2  

Interestingly, the results change dramatically if you replace:

有趣的是,如果你替换:

for i in 0 ..< n {
    let p = Int(arc4random_uniform(UInt32(array.count - i))) + i
    array[i] = array[p]
}

with

for i in 0 ..< n {
    let p = Int(arc4random_uniform(UInt32(array.count - i))) + i
    let tmp = array[p]
    array[i] = tmp
}

#1


2  

Interestingly, the results change dramatically if you replace:

有趣的是,如果你替换:

for i in 0 ..< n {
    let p = Int(arc4random_uniform(UInt32(array.count - i))) + i
    array[i] = array[p]
}

with

for i in 0 ..< n {
    let p = Int(arc4random_uniform(UInt32(array.count - i))) + i
    let tmp = array[p]
    array[i] = tmp
}