How do I get time of a Python program's execution?

后端 未结 30 1634
甜味超标
甜味超标 2020-11-22 02:20

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

30条回答
  •  甜味超标
    2020-11-22 02:53

    For the data folks using Jupyter Notebook

    In a cell, you can use Jupyter's %%time magic command to measure the execution time:

    %%time
    [ x**2 for x in range(10000)]
    

    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:

    # Convert your notebook to a .py script:
    !jupyter nbconvert --to script example_notebook.ipynb
    
    # Run the example_notebook with -t flag for time
    %run -t example_notebook
    

    Output

    IPython CPU timings (estimated):
      User   :       0.00 s.
      System :       0.00 s.
    Wall time:       0.00 s.
    

提交回复
热议问题