[Swift 开发] 数组 环形缓存区

[Swift 开发] 数组 环形缓存区

经过测试发现Swift数组并不是环形缓存区。

var aa = [0]

Duration.startMeasurement("Tough Math1")

for i in 0...100000
{
    aa.append(i)
}

Duration.stopMeasurement()

输出:
Tough Math1 took: 1.44ms

var aa = [0]

Duration.startMeasurement("Tough Math1") 

for i in 0...100000
{
    aa.insert(i, at: 0) 
}

Duration.stopMeasurement()

输出:
Tough Math1 took: 1591.72ms

var aa = [0]

Duration.startMeasurement("Tough Math1") 

for i in 0...100000
{
    aa.insert(i, at: aa.count / 2) 
}

Duration.stopMeasurement()

输出:
Tough Math1 took: 737.21ms

var aa = [0]

Duration.startMeasurement("Tough Math1") 

for i in 0...100000
{
    if aa.count > 2
    {
       aa.insert(i, at: aa.count - 2)
    }
    else
    {
       aa.insert(i, at: 0)
    }
}

Duration.stopMeasurement()

输出:
Tough Math1 took: 25.06ms

结果:
1. 从头部插入数据 耗时 1591.72ms

1. 从中部部插入数据 耗时 737.21ms

1. 从未部插入数据 耗时 25.06ms

Swift 数组好像并不是环形缓存区,看样子像线性的