Simple way to measure cell execution time in ipython notebook

后端 未结 12 1407
春和景丽
春和景丽 2020-11-29 15:17

I would like to get the time spent on the cell execution in addition to the original output from cell.

To this end, I tried %%timeit -r1 -n1 but it does

相关标签:
12条回答
  • I simply added %%time at the beginning of the cell and got the time. You may use the same on Jupyter Spark cluster/ Virtual environment using the same. Just add %%time at the top of the cell and you will get the output. On spark cluster using Jupyter, I added to the top of the cell and I got output like below:-

    [1]  %%time
         import pandas as pd
         from pyspark.ml import Pipeline
         from pyspark.ml.classification import LogisticRegression
         import numpy as np
         .... code ....
    
    Output :-
    
    CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
    Wall time: 1min 18s
    
    0 讨论(0)
  • 2020-11-29 16:00

    When in trouble what means what:

    ?%timeit or ??timeit

    To get the details:

    Usage, in line mode:
      %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
    or in cell mode:
      %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
      code
      code...
    
    Time execution of a Python statement or expression using the timeit
    module.  This function can be used both as a line and cell magic:
    
    - In line mode you can time a single-line statement (though multiple
      ones can be chained with using semicolons).
    
    - In cell mode, the statement in the first line is used as setup code
      (executed but not timed) and the body of the cell is timed.  The cell
      body has access to any variables created in the setup code.
    
    0 讨论(0)
  • 2020-11-29 16:04

    An easier way is to use ExecuteTime plugin in jupyter_contrib_nbextensions package.

    pip install jupyter_contrib_nbextensions
    jupyter contrib nbextension install --user
    jupyter nbextension enable execute_time/ExecuteTime
    
    0 讨论(0)
  • 2020-11-29 16:04

    Sometimes the formatting is different in a cell when using print(res), but jupyter/ipython comes with a display. See an example of the formatting difference using pandas below.

    %%time
    import pandas as pd 
    from IPython.display import display
    
    df = pd.DataFrame({"col0":{"a":0,"b":0}
                  ,"col1":{"a":1,"b":1}
                  ,"col2":{"a":2,"b":2}
                 })
    
    #compare the following
    print(df)
    display(df)
    

    The display statement can preserve the formatting.

    0 讨论(0)
  • 2020-11-29 16:08

    %time and %timeit now come part of ipython's built-in magic commands

    0 讨论(0)
  • 2020-11-29 16:09

    This is not exactly beautiful but without extra software

    class timeit():
        from datetime import datetime
        def __enter__(self):
            self.tic = self.datetime.now()
        def __exit__(self, *args, **kwargs):
            print('runtime: {}'.format(self.datetime.now() - self.tic))
    

    Then you can run it like:

    with timeit():
        # your code, e.g., 
        print(sum(range(int(1e7))))
    
    % 49999995000000
    % runtime: 0:00:00.338492
    
    0 讨论(0)
提交回复
热议问题