timeit库可以用来测试代码的执行效率
1 # -*- coding: utf-8 -*-
2 # @author: Tele
3 # @Time : 2019/04/20 下午 3:52
4 # timeit可以用来测试代码执行效率
5 import timeit
6
7
8 def test1():
9 li = list()
10 for i in range(0, 10000):
11 li.append(i)
12
13
14 def test2():
15 li = list()
16 for i in range(0, 10000):
17 li.insert(0, i)
18
19
20 timer1 = timeit.timeit("test1()", "from __main__ import test1", number=1000)
21 print("append:", timer1) # append: 1.1960364283501856 效率更高
22 timer2 = timeit.timeit("test2()", "from __main__ import test2", number=1000)
23 print("insert:", timer2) # insert: 31.762624102166527
原文:https://www.cnblogs.com/tele-share/p/10743250.html