0

I'm using the python hotshot profiler, and it tells me one of my methods foo() is being called N times, where N is a larger number than I was expecting.

Is there a way to get more detail about where foo() is being called from? Ideally, a listing of module names and line numbers?

(I can't just use grep. My codebase contains lots of calls to foo(), but I want to find only the ones that are actually being executed under the particular conditions I've set up in the profiler.)

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • You might try *[this](http://stackoverflow.com/questions/4295799/how-to-improve-performance-of-this-code/4299378#4299378)*. – Mike Dunlavey Nov 09 '11 at 22:20

1 Answers1

1

One option is to add some logging to the top of foo() to indicate where it was called from, you just need to add these lines:

def foo():
    import traceback
    print 'foo called from', traceback.extract_stack(limit=2)[0][2]
    # previous foo() code

Instead of logging you may find it more useful to maintain a global dictionary where you keep track of a count of how many times foo() has been called from different places.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • And if you want the whole call stack: `print "call stack: %s" % [caller[2] for caller in traceback.extract_stack()]` – John Gordon Nov 18 '11 at 17:52