1

In IPython, you can time a single line with the %timeit magic.

In jupyter, you can time an entire cell with the %%timeit magic.

However, what I'd like is for each line of a jupyter cell (or an entire python script) to be timed, without using %timeit for each line individually. When I'm writing python code that I want to run fast, a functionality like this would be wonderful, so I could tell which lines are taking the most time.

You may wonder what I want this to do for for loops or if statements, but for now, just assume that it's a really long script full of simple statements.

extremeaxe5
  • 723
  • 3
  • 13
  • 2
    Take a look at [`line_profiler`](https://github.com/rkern/line_profiler). – ForceBru Jun 24 '18 at 21:05
  • 2
    https://stackoverflow.com/questions/3927628/how-can-i-profile-python-code-line-by-line – whackamadoodle3000 Jun 24 '18 at 21:10
  • While it's good to have a general awareness of what's fast and what's slow, don't focus on it too much, especially during the early phases of development. Write correct, readable code, and once it works, then profile it to see where the performance bottlenecks are. As Knuth said, 97% of the time, premature optimization is the root of all evil. – PM 2Ring Jun 24 '18 at 21:14

0 Answers0