Herman Schaaf
Herman Schaaf

Reputation: 48485

Question regarding python profiling

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?

Upvotes: 2

Views: 215

Answers (2)

jchl
jchl

Reputation: 6542

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

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

Upvotes: 0

AndiDog
AndiDog

Reputation: 70239

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

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

Upvotes: 1

Related Questions