问题
I'd like to profile a method of a function in Python, using cProfile. I tried the following:
import cProfile as profile
# Inside the class method...
profile.run("self.myMethod()", "output_file")
But it does not work. How can I call a self.method with "run"?
回答1:
Use the profilehooks decorator
http://pypi.python.org/pypi/profilehooks
回答2:
EDIT: Sorry, didn't realise that the profile call was in a class method.
run
just tries to exec
the string you pass it. If self
isn't bound to anything in the scope of the profiler you are using, you can't use it in run
! Use the runctx method to pass in the local and global variables in the scope of the call to the profiler:
>>> import time
>>> import cProfile as profile
>>> class Foo(object):
... def bar(self):
... profile.runctx('self.baz()', globals(), locals())
...
... def baz(self):
... time.sleep(1)
... print 'slept'
... time.sleep(2)
...
>>> foo = Foo()
>>> foo.bar()
slept
5 function calls in 2.999 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 2.999 2.999 <stdin>:5(baz)
1 0.000 0.000 2.999 2.999 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 2.999 1.499 2.999 1.499 {time.sleep}
Notice the last line: time.sleep
is what's taking up the time.
回答3:
I wouldn't recommend profiling a single routine, because that implies knowing in advance there's a problem there.
A fundamental aspect of performance problems is they're sneaky. They're not where you think they are, because if they were you would have solved them already.
It's better to run the whole program with a realistic workload and let the profiling technique tell you where the problems are.
Here's an example where profiling finds the problem, and it is not where expected.
回答4:
If your function under profile returns value(s), you need to change the excellent answer from @katrielalex slightly:
... profile.runctx('val = self.baz()', globals(), locals())
... print locals()['val']
来源:https://stackoverflow.com/questions/4492535/profiling-a-method-of-a-class-in-python-using-cprofile