Question regarding python profiling – Education Career Blog

I’m trying to do profiling of my application in python. I’m using the cProfile library. I need to profile the onFrame function of my application, but this is called by an outside application. I’ve tried loads of things, but at the moment I have the following in my onFrame method:

runProfiler(self)

and then outside of my class I have the following:

o = None

def doProfile ():
    print "doProfile invoked"
    o.structure.updateOrders

def runProfiler(self):
    print "runProfiler invoked"
    o = self
    cProfile.run('doProfile()', 'profile.log')

If this seems strange, it’s because I’ve tried everything to get rid of the error “name doProfile not defined”. Even now, the runProfiler method gets called, and the “runProfiler invoked” gets printed, but then I get the error just described. What am I doing wrong?

,

In that case, you should pass the necessary context to cProfile.runctx:

cProfile.runctx("doProfile()", globals(), locals(), "profile.log")

,

An alternative is to use the runcall method of a Profile object.

profiler = cProfile.Profile()
profiler.runcall(doProfile)
profiler.dump_stats("profile.log")

Leave a Comment