【发布时间】:2017-04-03 00:22:41
【问题描述】:
这与this question 有点相关,其中假设使用生成器(迭代器)遍历嵌套数组对于遍历元素是最佳的,只要您不需要存储结果,如果您只想展平数组,则最好使用重复的数组连接。
然而,我决定做一些测试,并以惰性和存储形式实现这个函数(将包含 Ints 或 [Int]s 的数组 [Any] 展平),结果存储形式是更快,即使只是用于迭代元素!这意味着不知何故,迭代生成器比在内存中构造一个新数组和然后迭代那个花费更多的时间。
令人难以置信的是,它甚至比同一程序的 python 实现慢了大约 5-70%,后者随着输入的减少而恶化。 Swift 是使用 -O 标志构建的。
这里是三个测试用例 1. 小输入,混合; 2.大输入,[Int]显性,3.大输入,Int显性:
斯威夫特
let array1: [Any] = [Array(1...100), Array(101...105), 106,
Array(107...111), 112, 113, 114, Array(115...125)]
let array2: [Any] = Array(repeating: Array(1...5), count: 2000)
let array3: [Any] = Array(repeating: 31, count: 10000)
Python
A1 = [list(range(1, 101)), list(range(101, 106)), 106,
list(range(107, 112)), 112, 113, 114, list(range(115, 126))]
A2 = list(range(1, 6)) * 2000
A3 = [31] * 10000
生成器和数组构建器:
斯威夫特
func chain(_ segments: [Any]) -> AnyIterator<Int>{
var i = 0
var j = 0
return AnyIterator<Int> {
while i < segments.count {
switch segments[i] {
case let e as Int:
i += 1
return e
case let E as [Int]:
if j < E.count {
let val = E[j]
j += 1
return val
}
j = 0
i += 1
default:
return nil
}
}
return nil
}
}
func flatten_array(_ segments: [Any]) -> [Int] {
var result = [Int]()
for segment in segments {
switch segment {
case let segment as Int:
result.append(segment)
case let segment as [Int]:
result.append(contentsOf: segment)
default:
break
}
}
return result
}
Python
def chain(L):
for i in L:
if type(i) is int:
yield i
elif type(i) is list:
yield from i
def flatten_list(L):
result = []
for i in L:
if type(i) is int:
result.append(i)
elif type(i) is list:
result.extend(i)
return result
以及基准测试结果(第一个测试用例循环 100000 次,其他测试用例循环 1000 次):
斯威夫特
test case 1 (small mixed input)
Filling an array : 0.068221092224121094 s
Filling an array, and looping through it : 0.074559926986694336 s
Looping through a generator : 1.5902719497680664 s *
Materializing the generator to an array : 1.759943962097168 s *
test case 2 (large input, [Int] s)
Filling an array : 0.20634698867797852 s
Filling an array, and looping through it : 0.21031379699707031 s
Looping through a generator : 1.3505551815032959 s *
Materializing the generator to an array : 1.4733860492706299 s *
test case 3 (large input, Int s)
Filling an array : 0.27392101287841797 s
Filling an array, and looping through it : 0.27670192718505859 s
Looping through a generator : 0.85304021835327148 s
Materializing the generator to an array : 1.0027849674224854 s *
Python
test case 1 (small mixed input)
Filling an array : 0.1622014045715332 s
Filling an array, and looping through it : 0.4312894344329834 s
Looping through a generator : 0.6839139461517334 s
Materializing the generator to an array : 0.5300459861755371 s
test case 2 (large input, [int] s)
Filling an array : 1.029205083847046 s
Filling an array, and looping through it : 1.2195289134979248 s
Looping through a generator : 1.0876803398132324 s
Materializing the generator to an array : 0.8958714008331299 s
test case 3 (large input, int s)
Filling an array : 1.0181667804718018 s
Filling an array, and looping through it : 1.244570255279541 s
Looping through a generator : 1.1220412254333496 s
Materializing the generator to an array : 0.9486079216003418 s
显然,Swift 非常非常擅长构建数组。但是为什么它的生成器这么慢,在某些情况下甚至比 Python 的还要慢呢? (在表中用* 标记。)用非常大的输入(> 100,000,000 个元素,这几乎使 Swift 崩溃)进行测试表明,即使在极限情况下,生成器的速度也会比数组填充慢至少一倍最好的情况是 3.25。
如果这真的是该语言固有的,它有一些有趣的含义。例如,常识(对我作为一个 python 程序员来说)如果我们试图合成一个不可变对象(如字符串),我们应该首先将源提供给生成函数以展开它,然后将输出关闭到joined() 方法,该方法适用于单个浅序列。相反,看起来最有效的策略是通过数组进行序列化;将源展开到中间数组,然后从数组构造输出。
构建一个全新的数组,然后迭代它比在原始数组上进行惰性迭代更快吗?为什么?
(Possibly related javascript question)
编辑
这里是测试代码:
斯威夫特
func time(test_array: [Any], cycles: Int = 1000000) -> (array_iterate: Double,
array_store : Double,
generate_iterate: Double,
generate_store: Double) {
func start() -> Double { return Date().timeIntervalSince1970 }
func lap(_ t0: Double) -> Double {
return Date().timeIntervalSince1970 - t0
}
var t0 = start()
for _ in 0..<cycles {
for e in flatten_array(test_array) { e + 1 }
}
let ΔE1 = lap(t0)
t0 = start()
for _ in 0..<cycles {
let array: [Int] = flatten_array(test_array)
}
let ΔE2 = lap(t0)
t0 = start()
for _ in 0..<cycles {
let G = chain(test_array)
while let g = G.next() { g + 1 }
}
let ΔG1 = lap(t0)
t0 = start()
for _ in 0..<cycles {
let array: [Int] = Array(chain(test_array))
}
let ΔG2 = lap(t0)
return (ΔE1, ΔE2, ΔG1, ΔG2)
}
print(time(test_array: array1, cycles: 100000))
print(time(test_array: array2, cycles: 1000))
print(time(test_array: array3, cycles: 1000))
Python
def time_f(test_array, cycles = 1000000):
lap = lambda t0: time() - t0
t0 = time()
for _ in range(cycles):
for e in flatten_list(test_array):
e + 1
ΔE1 = lap(t0)
t0 = time()
for _ in range(cycles):
array = flatten_list(test_array)
ΔE2 = lap(t0)
t0 = time()
for _ in range(cycles):
for g in chain(test_array):
g + 1
ΔG1 = lap(t0)
t0 = time()
for _ in range(cycles):
array = list(chain(test_array))
ΔG2 = lap(t0)
return ΔE1, ΔE2, ΔG1, ΔG2
print(time_f(A1, cycles=100000))
print(time_f(A3, cycles=1000))
print(time_f(A2, cycles=1000))
【问题讨论】:
-
你的完整测试代码是什么?优化可能很棘手,因为有些行可能会被编译器优化掉
-
@CodeDifferent 添加到问题中
标签: arrays swift performance generator