How do I profile my time spent in library function

2019-07-21 11:00发布

问题:

When I try to profile a class, I am unable to break down the time spent past each of my own methods and functions.

For example, using cProfile, I am able to do the following:

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()")

I am given this analysis:

         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}

Alternatively, using profilehooks I am able to do the following:

@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()

And I am given this analysis:

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

Neither technique will show how much time was spent in library functions. How can I get an analysis that shows me how much time was spent, for instance, in numpy.add()?