gpt4 book ai didi

python - 在某些情况下,列表理解不一定比 for 循环方式更快,这是否合理?

转载 作者:行者123 更新时间:2023-11-28 17:57:19 25 4
gpt4 key购买 nike

python doc

List comprehensions provide a concise way to create lists.

Python 文档好像没有说(可能是这个说法不正确,如果需要请修正)

“列表理解比 for 循环更快”

这里有两个函数

def find_base_match_v1(char, matrix):
base_matches = []
for row_index, row in enumerate(matrix):
for column_index, column in enumerate(row):
if char == column:
base_matches.append((row_index, column_index))
return base_matches

def find_base_match_v2(char, matrix):
base_matches = [(row_index, column_index)
for row_index, row in enumerate(matrix)
for column_index, column in enumerate(row)
if char == column]
return base_matches

函数v1的性能(for循环方式)

timeit "find_base_match_v1('d', grid)"

10.6 ns ± 0.419 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

函数v2的性能(列表理解方法)

timeit "find_base_match_v2('d', grid)"

12.1 ns ± 1.74 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

有时结果相反,v2 比 v1 快一点。

无论如何,不​​能保证哪个一定比另一个快,我的理解对吗?

最佳答案

列表理解不是魔术。查看您的代码:它读取矩阵并存储等于给定字符的元素的坐标:

matrix1 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
print(timeit.timeit(lambda: find_base_match_v1('f', matrix1)))
# 1.002808532999552
print(timeit.timeit(lambda: find_base_match_v2('f', matrix1)))
# 1.0684777589995065

时间几乎相同,原因很明显:构建的列表很小

print(find_base_match_v1('f', matrix1))
# [(1, 2)]
print(find_base_match_v2('f', matrix1))
# [(1, 2)]

如果你构建一个更大的列表,假设有一百个元素,列表理解胜过 for 循环(Ubuntu 上的 CPython 3.6):

matrix2 = [['f']*10 for _ in range(10)]
print(find_base_match_v1('f', matrix2))
# [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]

print(timeit.timeit(lambda: find_base_match_v1('f', matrix2)))
# 9.212440792000052
print(timeit.timeit(lambda: find_base_match_v2('f', matrix2)))
# 6.918398160998549

但是正如评论中所写,文档并不能保证这一点。有关详细信息,请参阅:Are list-comprehensions and functional functions faster than "for loops"?

关于python - 在某些情况下,列表理解不一定比 for 循环方式更快,这是否合理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57522432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com