Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何分析花在库函数上的时间?_Python_Profiling_Python 3.4_Cprofile - Fatal编程技术网

Python 我如何分析花在库函数上的时间?

Python 我如何分析花在库函数上的时间?,python,profiling,python-3.4,cprofile,Python,Profiling,Python 3.4,Cprofile,当我试图分析一个类时,我无法分解我自己的每个方法和函数所花费的时间 例如,使用cProfile,我可以执行以下操作: import numpy as np import cProfile class Test_Class: def __init__(self, length, width): self.length = length self.width = width self.populate() return Non

当我试图分析一个类时,我无法分解我自己的每个方法和函数所花费的时间

例如,使用
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()")
@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()
我得到的分析是:

         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}
*** 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)
我得到了这样的分析:

         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}
*** 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)

这两种技术都无法显示在库函数中花费了多少时间。我怎样才能得到一个分析来显示我花了多少时间,例如,在
numpy.add()

相关:(无答案)相关:(答案没有比上面更深入的描述)也相关:。你有什么特别的理由认为答案会改变吗?@jornsharpe不相信这些答案能满足我的问题。我已经尝试过按照建议使用profilehooks库,但没有成功,我的实际用例太大,无法将每个操作封装在自己的函数中。这并不能真正回答我的问题!为什么你期望这个问题会得到与之前许多类似问题不同的答案?如果你尝试了一些没有成功的事情,为什么不用一个简单的问题来回答呢?