profiling a method of a class in Python using cProfile?

Question:

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”?

Asked By: user248237

||

Answers:

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.

Answered By: Katriel

Use the profilehooks decorator

http://pypi.python.org/pypi/profilehooks

Answered By: Falmarri

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.

Answered By: Mike Dunlavey

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']
Answered By: sqqqrly
  import cProfile
  p = cProfile.Profile()
  p.runcall(self.myMethod)
  p.print_stats()

The Profile class is documented here.

Answered By: Julius Kunze
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.