1.timeit:
-
>>> import timeit
-
>>> def fun():
-
for i in range(100000):
-
a = i * i
-
>>> timeit.timeit('fun()', 'from __main__ import fun', number=1)
-
0.02922706632834235
-
>>>
timeit只输出被测试代码的总运行时间,单位为秒,没有详细的统计。
2.profile
profile:纯Python实现的性能测试模块,接口和cProfile一样。
-
>>> import profile
-
>>> def fun():
-
for i in range(100000):
-
a = i * i
-
>>> profile.run('fun()')
-
5 function calls