views:

207

answers:

1

How do I profile a call that involves self and arguments in python?

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

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

In the above example how would I profile just the call to profileCommand? I figured out I need to use runctx for argument, but how do I deal with self? (The code actually involves a UI, so it's hard to pull out the call to profile separately).

+1  A: 

you need to pass locals/globals dict and pass first argument what you will usually type e.g.

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 don't call whole UI in profile , profile the specific function or computation

Anurag Uniyal