這時候可以使用內建的cProfile模組來看每個function call所需的時間
例如:
cProfile.run( 'function()' )
可以看到像下面這樣的列表
42333 function calls (42317 primitive calls) in 0.489 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.489 0.489
2 0.000 0.000 0.001 0.000 arrayprint.py:175(_array2string)
32 0.000 0.000 0.000 0.000 arrayprint.py:208(
2 0.000 0.000 0.000 0.000 arrayprint.py:22(product)
6/2 0.000 0.000 0.001 0.000 arrayprint.py:246(array2string)
32 0.000 0.000 0.000 0.000 arrayprint.py:312(_extendLine)
10/2 0.000 0.000 0.000 0.000 arrayprint.py:320(_formatArray)
32 0.000 0.000 0.000 0.000 arrayprint.py:493(_formatInteger)
能幫助分析是哪個函式造成效能瓶頸, 如果是使用物件的方法的話則必須寫成:
cProfile.runctx('self.method()', globals(), locals())
否則cProfile會認不到method所在的scope