标签:end 列表 运行时 tle 运行 模块 print func 生成式
import timeit
>>> func = '''
... arr = []
... for i in range(1000):
... arr.append(i)
... '''
>>> t1 = timeit.timeit(stmt=func, number=10000)
>>> t2 = timeit.timeit(stmt="[i for i in range(1000)]", number=10000)
>>> t1
1.0912232999999105
>>> t2
0.5270981999999549
>>> def func(num=3):
... for i in range(num):
... print(f"Repeat for {i}.")
...
>>>
>>> t = timeit.timeit(stmt=func, number=5)
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
>>> t
0.0009049000000231899
>>> t = timeit.timeit(stmt=func, setup="func"+"num=5", number=5)
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
>>> t
0.008177499999874271
t = timeit.timeit(func, setup="funcnum=5", number=5)
也行,但不直观>>> s = '''
... def func(num):
... for i in range(num):
... print(f"Repeat for {i}")
... '''
>>> t = timeit.timeit(stmt="func(num)", setup=s+"num=3", number=5)
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
>>> t
0.0020025999997415056
标签:end 列表 运行时 tle 运行 模块 print func 生成式
原文地址:https://www.cnblogs.com/yorkyu/p/12037517.html