Simple way to measure cell execution time in ipython notebook

后端 未结 12 1426
春和景丽
春和景丽 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条回答
  •  野性不改
    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.

提交回复
热议问题