5

I've written a Python script, but running it is taking a lot longer than I had anticipated, and I've no obvious candidate for particuklar lines in the script taking up runtime.

Is there anything I can put in my code to check how long its taking to run through each line?

Many thanks.

Shai
  • 111,146
  • 38
  • 238
  • 371
user124123
  • 1,642
  • 7
  • 30
  • 50

3 Answers3

6

Have you tried running python with profiling?

python -m cProfile --sort cumulative your_single_thread_script.py &> out.log

You can find more details in this question How can you profile a python script?

You can read more about the profiling utility here.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • what about if I had one script calling a bunch of others, how could I check the time for each individual script? – user124123 Jun 26 '13 at 08:56
4

Use a profiler such as hotshot. It's easy!

1) run your code with the profiles:

import hotshot

prof = hotshot.Profile("hotshot_stats.prof")
prof.runcall(my_function)
prof.close()

2) Read the resulting file:

from hotshot import stats

s = stats.load("hotshot_stats.prof")
s.strip_dirs()
s.sort_stats('time', 'calls')
s.print_stats(20)
nst
  • 3,862
  • 1
  • 31
  • 40
3

timeit is a standard module since python 2.3 take a look at the documentation for it.

Shai
  • 111,146
  • 38
  • 238
  • 371
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73