Profiling yourself and arguments in python?

How to profile a call that includes self and arguments in python?

def performProfile(self):
    import cProfile
    self.profileCommand(1000000)

def profileCommand(self, a):
    for i in a:
        pass

In the example above, how would I profile only the profileCommand function call? I realized that I need to use runctx for argumentation, but how can I deal with myself? (The code does include a user interface, so it’s hard to pull the call to the profile separately).

+3
source share
1 answer

you need to pass the locals / globals dict and pass the first argument that you usually type for example.

cProfile.runctx("self.profileCommand(100)", globals(),locals())

use something like this

class A(object):
    def performProfile(self):
        import cProfile
        cProfile.runctx("self.profileCommand(100)", globals(),locals())

    def profileCommand(self, a):
        for i in xrange(a):
            pass
        print "end."

A().performProfile()

and do not call the entire user interface in the profile, the profile of a particular function or calculation

+11
source

Source: https://habr.com/ru/post/1711061/


All Articles