Python学习笔记:利用timeit计算函数调用耗时
作者:互联网
Python学习笔记:利用timeit计算函数调用耗时
一、timeit模块简介
Timer对象包含两个参数:第一个参数是你想要计时的Python语句,第二个参数运行一次来建立测试。
默认情况timeit会执行一百万次返回多少秒,可以理解为执行一次返回多少微秒。timeit有一个number参数,可以设置为1000,那么返回时间的单位就是毫秒了。
二、案例演示
1、阶乘耗时
运行结果:
1! executing time: 0.0006161999999960699 milliseconds
2! executing time: 0.0006439999999940937 milliseconds
3! executing time: 0.0006774000000007163 milliseconds
4! executing time: 0.0007241999999791915 milliseconds
5! executing time: 0.0007625000000075488 milliseconds
6! executing time: 0.0014218999999968673 milliseconds
7! executing time: 0.0008945999999809828 milliseconds
8! executing time: 0.0009628999999904408 milliseconds
9! executing time: 0.0010600999999894611 milliseconds
10! executing time: 0.0011124000000108936 milliseconds
11! executing time: 0.0027358999999762545 milliseconds
12! executing time: 0.0012527000000090993 milliseconds
13! executing time: 0.0012741000000175973 milliseconds
14! executing time: 0.0023416999999881227 milliseconds
15! executing time: 0.0027690000000006876 milliseconds
16! executing time: 0.0037630999999862524 milliseconds
17! executing time: 0.0016303999999820462 milliseconds
18! executing time: 0.0017330000000015389 milliseconds
19! executing time: 0.0022575999999787655 milliseconds
20! executing time: 0.0028788999999846965 milliseconds
21! executing time: 0.002625600000015993 milliseconds
22! executing time: 0.0020728000000076463 milliseconds
23! executing time: 0.0022077000000138014 milliseconds
24! executing time: 0.002774299999998675 milliseconds
25! executing time: 0.002434599999986631 milliseconds
26! executing time: 0.002475099999998065 milliseconds
27! executing time: 0.0030888000000004467 milliseconds
28! executing time: 0.0026891999999918426 milliseconds
29! executing time: 0.003031700000008186 milliseconds
30! executing time: 0.0029704000000094766 milliseconds
31! executing time: 0.0036570999999980813 milliseconds
32! executing time: 0.0029528999999968164 milliseconds
33! executing time: 0.0033359999999902357 milliseconds
34! executing time: 0.0032547000000135995 milliseconds
35! executing time: 0.005373399999996309 milliseconds
36! executing time: 0.00503609999998389 milliseconds
37! executing time: 0.00511069999998881 milliseconds
38! executing time: 0.003444900000005191 milliseconds
39! executing time: 0.003606900000022506 milliseconds
40! executing time: 0.004605599999990773 milliseconds
41! executing time: 0.006797500000004675 milliseconds
42! executing time: 0.006650899999982585 milliseconds
43! executing time: 0.0039322999999740205 milliseconds
44! executing time: 0.00446339999999168 milliseconds
45! executing time: 0.004669699999993782 milliseconds
46! executing time: 0.005191400000001067 milliseconds
47! executing time: 0.004766899999992802 milliseconds
48! executing time: 0.004702500000007603 milliseconds
49! executing time: 0.004529599999983702 milliseconds
50! executing time: 0.004823899999990999 milliseconds
51! executing time: 0.00676319999999464 milliseconds
52! executing time: 0.007806900000019823 milliseconds
53! executing time: 0.007745200000016439 milliseconds
54! executing time: 0.005193399999996018 milliseconds
55! executing time: 0.0075526000000252225 milliseconds
56! executing time: 0.008500800000007303 milliseconds
57! executing time: 0.006352100000015071 milliseconds
58! executing time: 0.009804000000002588 milliseconds
59! executing time: 0.005712499999987131 milliseconds
60! executing time: 0.005463999999989255 milliseconds
61! executing time: 0.006630599999994047 milliseconds
62! executing time: 0.00636679999999501 milliseconds
63! executing time: 0.0069296000000065305 milliseconds
64! executing time: 0.006213199999990593 milliseconds
65! executing time: 0.008552299999990964 milliseconds
66! executing time: 0.00903500000001145 milliseconds
67! executing time: 0.010822299999972529 milliseconds
68! executing time: 0.008465499999999793 milliseconds
69! executing time: 0.012770700000004354 milliseconds
70! executing time: 0.013791300000008277 milliseconds
71! executing time: 0.009824400000013611 milliseconds
72! executing time: 0.010767999999984568 milliseconds
73! executing time: 0.010198600000023816 milliseconds
74! executing time: 0.013151499999992211 milliseconds
75! executing time: 0.010155600000018694 milliseconds
76! executing time: 0.008703800000006368 milliseconds
77! executing time: 0.007793899999995801 milliseconds
78! executing time: 0.009978199999977733 milliseconds
79! executing time: 0.009008399999999028 milliseconds
80! executing time: 0.008965000000017653 milliseconds
81! executing time: 0.008167799999995395 milliseconds
82! executing time: 0.00777859999999464 milliseconds
83! executing time: 0.009927000000004682 milliseconds
84! executing time: 0.008234199999975544 milliseconds
85! executing time: 0.00891070000000127 milliseconds
86! executing time: 0.008395799999988185 milliseconds
87! executing time: 0.009561800000000176 milliseconds
88! executing time: 0.009507700000000341 milliseconds
89! executing time: 0.008751200000006065 milliseconds
90! executing time: 0.008993899999978794 milliseconds
91! executing time: 0.008987399999995205 milliseconds
92! executing time: 0.011652599999990798 milliseconds
93! executing time: 0.01211399999999685 milliseconds
94! executing time: 0.010191499999990583 milliseconds
95! executing time: 0.009391899999997122 milliseconds
96! executing time: 0.009752800000001116 milliseconds
97! executing time: 0.011227800000000343 milliseconds
98! executing time: 0.010653800000000047 milliseconds
99! executing time: 0.011744800000002442 milliseconds
2、选择排序法耗时
运行结果:
2 numbers sorting time: 0.0012724999999988995 milliseconds
3 numbers sorting time: 0.0020489999999995234 milliseconds
4 numbers sorting time: 0.005885899999981348 milliseconds
5 numbers sorting time: 0.004977900000000091 milliseconds
6 numbers sorting time: 0.004677700000002005 milliseconds
7 numbers sorting time: 0.008602999999993699 milliseconds
8 numbers sorting time: 0.00920339999998987 milliseconds
9 numbers sorting time: 0.010189900000000307 milliseconds
10 numbers sorting time: 0.01227449999998953 milliseconds
11 numbers sorting time: 0.012929200000002083 milliseconds
12 numbers sorting time: 0.014420499999999947 milliseconds
13 numbers sorting time: 0.019797500000009904 milliseconds
14 numbers sorting time: 0.02116119999999455 milliseconds
15 numbers sorting time: 0.020406900000011774 milliseconds
16 numbers sorting time: 0.02709310000000187 milliseconds
17 numbers sorting time: 0.02739859999999794 milliseconds
18 numbers sorting time: 0.029796199999992723 milliseconds
19 numbers sorting time: 0.027377799999982244 milliseconds
20 numbers sorting time: 0.029248899999998912 milliseconds
21 numbers sorting time: 0.03271739999999568 milliseconds
22 numbers sorting time: 0.03371390000000929 milliseconds
23 numbers sorting time: 0.03946960000001809 milliseconds
24 numbers sorting time: 0.047130800000019235 milliseconds
25 numbers sorting time: 0.047065599999996266 milliseconds
26 numbers sorting time: 0.04654649999997673 milliseconds
27 numbers sorting time: 0.05025089999998045 milliseconds
28 numbers sorting time: 0.0619158000000084 milliseconds
29 numbers sorting time: 0.0701507999999933 milliseconds
30 numbers sorting time: 0.060636599999980945 milliseconds
31 numbers sorting time: 0.07053490000001261 milliseconds
32 numbers sorting time: 0.073296699999986 milliseconds
33 numbers sorting time: 0.0809247000000255 milliseconds
34 numbers sorting time: 0.07596179999998753 milliseconds
35 numbers sorting time: 0.07587219999999206 milliseconds
36 numbers sorting time: 0.07948309999997605 milliseconds
37 numbers sorting time: 0.08802670000000035 milliseconds
38 numbers sorting time: 0.100508700000006 milliseconds
39 numbers sorting time: 0.09315509999998994 milliseconds
40 numbers sorting time: 0.10121169999999324 milliseconds
41 numbers sorting time: 0.10298099999999977 milliseconds
42 numbers sorting time: 0.10794119999999907 milliseconds
43 numbers sorting time: 0.13089430000002267 milliseconds
44 numbers sorting time: 0.13083069999998997 milliseconds
45 numbers sorting time: 0.1270202999999981 milliseconds
46 numbers sorting time: 0.12131460000000516 milliseconds
47 numbers sorting time: 0.14594470000000115 milliseconds
48 numbers sorting time: 0.1449931999999876 milliseconds
49 numbers sorting time: 0.14965540000000033 milliseconds
50 numbers sorting time: 0.16672389999999382 milliseconds
51 numbers sorting time: 0.1567258000000038 milliseconds
52 numbers sorting time: 0.15108240000000706 milliseconds
53 numbers sorting time: 0.1738794999999982 milliseconds
54 numbers sorting time: 0.1928357000000176 milliseconds
55 numbers sorting time: 0.19325870000000123 milliseconds
56 numbers sorting time: 0.19082629999999767 milliseconds
57 numbers sorting time: 0.21581030000001533 milliseconds
58 numbers sorting time: 0.2127691000000027 milliseconds
59 numbers sorting time: 0.22047800000001416 milliseconds
60 numbers sorting time: 0.20358969999998067 milliseconds
61 numbers sorting time: 0.21165640000000963 milliseconds
62 numbers sorting time: 0.21881680000001325 milliseconds
63 numbers sorting time: 0.23560399999999504 milliseconds
64 numbers sorting time: 0.25244480000000635 milliseconds
65 numbers sorting time: 0.26406789999998637 milliseconds
66 numbers sorting time: 0.25990409999999997 milliseconds
67 numbers sorting time: 0.26312540000000695 milliseconds
68 numbers sorting time: 0.2712624000000119 milliseconds
69 numbers sorting time: 0.28340520000000424 milliseconds
70 numbers sorting time: 0.2923759999999902 milliseconds
71 numbers sorting time: 0.2992949000000067 milliseconds
72 numbers sorting time: 0.3073478000000023 milliseconds
73 numbers sorting time: 0.34134579999999914 milliseconds
74 numbers sorting time: 0.3125430000000051 milliseconds
75 numbers sorting time: 0.33875150000000076 milliseconds
76 numbers sorting time: 0.3322732999999971 milliseconds
77 numbers sorting time: 0.3412600999999995 milliseconds
78 numbers sorting time: 0.38342039999997723 milliseconds
79 numbers sorting time: 0.36002880000000914 milliseconds
80 numbers sorting time: 0.34170640000002095 milliseconds
81 numbers sorting time: 0.3924379999999985 milliseconds
82 numbers sorting time: 0.39715799999999035 milliseconds
83 numbers sorting time: 0.4302678999999898 milliseconds
84 numbers sorting time: 0.37184500000000753 milliseconds
85 numbers sorting time: 0.4156522000000109 milliseconds
86 numbers sorting time: 0.42353549999998563 milliseconds
87 numbers sorting time: 0.45001400000001013 milliseconds
88 numbers sorting time: 0.43643330000000446 milliseconds
89 numbers sorting time: 0.4744289000000208 milliseconds
90 numbers sorting time: 0.4462363000000096 milliseconds
91 numbers sorting time: 0.48084140000000275 milliseconds
92 numbers sorting time: 0.45832629999998176 milliseconds
93 numbers sorting time: 0.5081308000000035 milliseconds
94 numbers sorting time: 0.504677700000002 milliseconds
95 numbers sorting time: 0.4975833000000023 milliseconds
96 numbers sorting time: 0.5010714000000007 milliseconds
97 numbers sorting time: 0.5380179999999939 milliseconds
98 numbers sorting time: 0.5840522000000021 milliseconds
99 numbers sorting time: 0.5725185999999951 milliseconds
3、测试列表性能
运行结果:
concat: 1.97066140000004 milliseconds
append: 0.10303559999999834 milliseconds
comprehension: 0.05253430000004755 milliseconds
list range: 0.018974300000081712 milliseconds
x.pop(0) time: 2.162285499999939 milliseconds
x.pop() time: 0.00032129999999597203 milliseconds
可以看到从列表头弹出元素比从列表尾弹出元素耗时多得多……
列表各种方法的时间复杂度
标签:timeit,sorting,Python,函数调用,executing,numbers,time,milliseconds 来源: https://blog.csdn.net/howard2005/article/details/102731918