timeit_list操作测试

'''
timeit库Timer函数
'''
from timeit import Timer
def test1():
    l = list(range(1000))
def test2():
    l = []
    for i in range(1000):
        l.append(i)
def test3():
    l = []
    for i in range(1000):
        l = l + [i]
def test4():
    l = [i for i in range(1000)]

if __name__ == '__main__':
    # Timer函数,函数名,导入包
    t1 = Timer("test1()","from __main__ import test1")
    # timeit运行次数
    print(t1.timeit(number = 1000))

    t2 = Timer("test2()","from __main__ import test2")
    print(t2.timeit(number =1000))

    t3 = Timer("test3","from __main__ import test3")
    print(t3.timeit(number=1000))

    t4 = Timer("test4","from __main__ import test4")
    print(t4.timeit(number=1000))

2020-05-08

posted @ 2020-05-08 00:33  Hany47315  阅读(196)  评论(0编辑  收藏  举报