小编Ric*_*ard的帖子

与Python代码相比,库itertools的性能

作为我的问题的答案找到两个列表相同的基于1的位置我得到了使用C库itertools来加快速度的提示.

要验证我使用cProfile编写了以下测试:

from itertools import takewhile, izip

def match_iter(self, other):
    return sum(1 for x in takewhile(lambda x: x[0] == x[1],
                                        izip(self, other)))

def match_loop(self, other):
    element = -1
    for element in range(min(len(self), len(other))):
        if self[element] != other[element]:
            element -= 1
            break
    return element +1

def test():
    a = [0, 1, 2, 3, 4]
    b = [0, 1, 2, 3, 4, 0]

    print("match_loop a=%s, b=%s, result=%s" % (a, b, match_loop(a, b)))
    print("match_iter a=%s, b=%s, result=%s" % (a, b, match_iter(a, …
Run Code Online (Sandbox Code Playgroud)

python performance python-itertools cprofile

4
推荐指数
2
解决办法
3265
查看次数

标签 统计

cprofile ×1

performance ×1

python ×1

python-itertools ×1