📜  扩展与追加循环的性能 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:46.046000             🧑  作者: Mango

代码示例1
# TLDR; Extend is faster than a an append loop
def append(alist, iterable):
    for item in iterable:
        alist.append(item)
        
def extend(alist, iterable):
    alist.extend(iterable)

import timeit

>>> min(timeit.repeat(lambda: append([], "abcdefghijklmnopqrstuvwxyz")))
2.867846965789795
>>> min(timeit.repeat(lambda: extend([], "abcdefghijklmnopqrstuvwxyz")))
0.8060121536254883