python – 在这个itertools配方中定义seen_add = seen.add有什么意义?

我正在阅读itertools recipe for unique_everseen

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

在上面的代码中定义seen_add = seen.add有什么意义?

解决方法:

性能.使用本地名称取消引用该方法要比属性查找(每次必须绑定一个新方法对象)快得多:

>>> import timeit
>>> timeit.timeit('s.add', 's = set()', number=10**7)
0.4227792940218933
>>> timeit.timeit('seen_add', 's = set(); seen_add = s.add', number=10**7)
0.15441945398924872

使用本地参考几乎快3倍.因为set.add用在循环中,所以值得优化属性查找.

上一篇:Python笛卡儿的产品和条件?


下一篇:itertools.chain()and itertools.product()操作+pandas.DataFrame.transform