0

I've written a python script and it's conducting a lot of string comparisons with a number of functions. If I were to run the current process it would take a month to complete. I would like to make the code more efficient but am not sure where I should focus.

Is there a way of having python tell you how long it takes for it to go through each of the functions?

Obviously, having it calculate the time it takes to process the function will take up resources.

thank you

dan
  • 13
  • 1
  • 2
    The term you're looking for is [**profiling**](http://docs.python.org/2/library/profile.html). – Blender Apr 18 '13 at 17:53

2 Answers2

1

What you're looking for is a profiler, here's a good place to start: http://docs.python.org/2/library/profile.html

Steve
  • 1,215
  • 6
  • 11
  • +1. But if it takes a month for the program to finish, it will take at least a month, possibly 10x longer, for it to profile! So, the OP still needs to factor out the code into smaller, separately-testable pieces so you can profile those pieces. (Of course there are all kinds of other advantages of doing that, so it's not wasted time.) – abarnert Apr 18 '13 at 18:03
  • Running if for an hour and hitting Ctrl-C usually works for me. Once the slow parts have been found then those can be separated out and tuned as needed. Although the best optimisation is often to reduce calls to the slow bit rather than speed it up. – Steve Apr 18 '13 at 18:07
  • Well, it depends on what the code is doing. If you've got a month-long program that you're trying to reduce to a day, it's possible that an hour in (especially with profiling enabled) you haven't even hit the slow part… – abarnert Apr 18 '13 at 18:17
0

What you're searching for is a profiler:

A profile is a set of statistics that describes how often and for how long various parts of the program executed

aldeb
  • 6,588
  • 5
  • 25
  • 48