计算python内部数据结构时间效率-源代码
#Author:qinjiaxi '''本程序计算各种循环的时间效率''' from timeit import Timer def test1(n): li = [] for i in range(n*1000): li = li +[i] return li def test2(n): li = [] li = [i for i in range(n*1000)] return li def test3(n): li = [] for i in range(n*1000): li.append(i) return li def test4(n): li = [] ii = list(range(n*1000)) return li t1 = Timer('test1(10)', 'from __main__ import test1') print('+:', t1.timeit(1000)) t2 = Timer('test2(10)', 'from __main__ import test2') print('[]:', t2.timeit(1000)) t3 = Timer('test3(10)', 'from __main__ import test3') print('append:', t3.timeit(1000)) t4 = Timer('test4(10)', 'from __main__ import test4') print('range:', t4.timeit(1000))
转载请注明出处
每天努力多一点,忧愁少一点,快乐多一点
--->by晴朗sky