当我尝试描述一个课程时,我无法分解我自己的每个方法和函数所花费的时间。
例如,使用Load
,我可以执行以下操作:
cProfile
我得到了这个分析:
import numpy as np
import cProfile
class Test_Class:
def __init__(self, length, width):
self.length = length
self.width = width
self.populate()
return None
def populate(self):
self.array = np.random.randint(0, 3, (self.length, self.width))
return None
def add(self, additional_array):
self.array = np.add(self.array, additional_array)
return None
def Test_Function():
x, y = 3000, 2000
test_array = np.random.randint(0, 2, (x, y))
model_one = Test_Class(x, y)
model_one.add(test_array)
cProfile.run("Test_Function()")
或者,使用 9 function calls in 0.214 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.002 0.002 0.214 0.214 <string>:1(<module>)
1 0.000 0.000 0.119 0.119 temp.py:11(populate)
1 0.020 0.020 0.020 0.020 temp.py:15(add)
1 0.002 0.002 0.212 0.212 temp.py:24(Test_Function)
1 0.000 0.000 0.119 0.119 temp.py:5(__init__)
1 0.000 0.000 0.214 0.214 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 0.190 0.095 0.190 0.095 {method 'randint' of 'mtrand.RandomState' objects}
我可以执行以下操作:
profilehooks
我得到了这个分析:
@profile(immediate=True)
def Test_Function():
x, y = 3000, 2000
test_array = np.random.randint(0, 2, (x, y))
model_one = Test_Class(x, y)
model_one.add(test_array)
Test_Function()
两种技术都不会显示在库函数中花费了多少时间。我怎样才能得到一个分析,告诉我花了多少时间,例如*** PROFILER RESULTS ***
Test_Function (C:/Users/mack/Desktop/temp2.py:19)
function called 1 times
7 function calls in 0.205 seconds
Ordered by: cumulative time, internal time, call count
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.205 0.205 temp.py:19(Test_Function)
2 0.185 0.093 0.185 0.093 {method 'randint' of 'mtrand.RandomState' objects}
1 0.000 0.000 0.113 0.113 temp.py:5(__init__)
1 0.000 0.000 0.113 0.113 temp.py:11(populate)
1 0.020 0.020 0.020 0.020 temp.py:15(add)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
0 0.000 0.000 profile:0(profiler)
?