Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling self and arguments in python?

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

like image 601
Dan Avatar asked Dec 14 '22 03:12

Dan


1 Answers

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

like image 112
Anurag Uniyal Avatar answered Dec 23 '22 15:12

Anurag Uniyal