0

I need to save entire function calls (and if possible - even lines) with time of call of a very large python program. I tried using trace.Trace() with count=1 and received dozen of files with count of each executed line per file, but no timing there. I can't see who called who and in which order. This should be for a program without exception. Any ideas how to do that? * I'm using Python 2.7.

martineau
  • 119,623
  • 25
  • 170
  • 301
chuk kik
  • 31
  • 1
  • 3
  • If you want timing information, see [How can you profile a script?](https://stackoverflow.com/questions/582336/how-can-you-profile-a-script) – martineau Oct 17 '18 at 13:43
  • well, I tried using cprofile, and even got all callers using pstats.Stats().print_callers(), but the only missing thing for me is the chronological order. From what I see, the sort options doesn't include such option. – chuk kik Oct 21 '18 at 17:23
  • I don't see any way of getting that kind of timing info from the profiler. A different approach would be to use [`sys.settrace`](https://docs.python.org/2/library/sys.html#sys.settrace) and hook it up to your own trace function that recorded the time each call occurred. The answer to the question [How do I print functions as they are called](https://stackoverflow.com/questions/8315389/how-do-i-print-functions-as-they-are-called) has an example of defining a function that can be used as `settrace`'s _tracefunc_ argument. It's also possible to get the source code of the line containing the call. – martineau Oct 21 '18 at 17:54
  • [Here's some more information](https://pymotw.com/2/sys/tracing.html) about `settrace()`. – martineau Oct 21 '18 at 18:06
  • Thanks a lot for the help. I already tried using settrace() but it doesn't work for me as well cause it slows down my program too much. I wanted as little as possible to interfere the execution time. Seems like there isn't a way to do what I'm looking for... – chuk kik Oct 22 '18 at 07:18
  • There's going to be some overhead involved in doing something like this—no way around that. The best you can do is mitigate it. Speaking of which, did you see the part the second linked answer about using `sys.setprofile()` instead of `settrace()`? – martineau Oct 22 '18 at 13:18

1 Answers1

0

Write trace.Trace(timing=True) to get the timing. On the command line, use the -g option.

I found this information by reading the trace documentation (Python 2 version). This should always be your first port of call when you think "how do I do a thing with [xyz]?".

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62