python timeit模块

 1 import timeit
 2 import math
 3 
 4 
 5 def myfun():
 6     for i in range(100):
 7         for j in range(2, 10):
 8             math.pow(i, 1/j)
 9 
10 timeitObj = timeit.Timer(stmt=myfun)
11 t1 = timeitObj.timeit(number=10000)
12 print(t1)  # 单位为秒
13 
14 t2 = timeitObj.repeat(number=10000, repeat=3)
15 print(t2)  # 单位为秒
16 
17 t3 = timeit.timeit(stmt=myfun, number=10000)
18 print(t3)
19 
20 t4 = timeit.repeat(stmt=myfun, number=10000, repeat=5)
21 print(t4)
原文地址:https://www.cnblogs.com/gundan/p/8316799.html