I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.
I've looked at the timeit
module, but it seems it's only for small snippets of code. I want to time the whole program.
I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.
I've looked at the timeit
module, but it seems it's only for small snippets of code. I want to time the whole program.
I used a very simple function to timing a part of code execution:
And to use it, just call it before the code to measure to retrieve timing function, then call the function after the code with comments, and the time will appear in front of the comments, for example:
Then the output will look like this:
I feel a little bit elegant this way.
Just Use
timeit
module. It works with both Python 2 And Python 3It returns in Seconds and you can have your Execution Time. Simple but you should write these in Main Function which starts program execution. If you want to get the Execution time even when you get error then take your parameter "Start" to it and calculate there like
Use line_profiler.
line_profiler will profile the time individual lines of code take to execute. The profiler is implemented in C via Cython in order to reduce the overhead of profiling.
The results will be:
The solution of rogeriopvl works fine, but if you want more specific info you can use the python built-in profiler. Check this page:
http://docs.python.org/library/profile.html
a profiler tells you a lot of useful information like the time spent in every function
For the data folks using Jupyter Notebooks
In a cell, you can use Jupyter's
%%time
magic command to measure the execution time:Output
CPU times: user 4.54 ms, sys: 0 ns, total: 4.54 ms
Wall time: 4.12 ms
This will only capture the execution time of a particular cell. If you'd like to capture the execution time of the whole notebook (i.e. program), you can create a new notebook in the same directory and in the new notebook execute all cells:
Suppose the notebook above is called
example_notebook.ipynb
. In a new notebook within the same directory:Output
IPython CPU timings (estimated): User : 0.00 s.
System : 0.00 s.
Wall time: 0.00 s.