Simple way to measure cell execution time in ipython notebook

后端 未结 12 1406
春和景丽
春和景丽 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 15:44

    you may also want to look in to python's profiling magic command %prunwhich gives something like -

    def sum_of_lists(N):
        total = 0
        for i in range(5):
            L = [j ^ (j >> i) for j in range(N)]
            total += sum(L)
        return total
    

    then

    %prun sum_of_lists(1000000)
    

    will return

    14 function calls in 0.714 seconds  
    
    Ordered by: internal time      
    
    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        5    0.599    0.120    0.599    0.120 <ipython-input-19>:4(<listcomp>)
        5    0.064    0.013    0.064    0.013 {built-in method sum}
        1    0.036    0.036    0.699    0.699 <ipython-input-19>:1(sum_of_lists)
        1    0.014    0.014    0.714    0.714 <string>:1(<module>)
        1    0.000    0.000    0.714    0.714 {built-in method exec}
    

    I find it useful when working with large chunks of code.

    0 讨论(0)
  • 2020-11-29 15:45

    Use cell magic and this project on github by Phillip Cloud:

    Load it by putting this at the top of your notebook or put it in your config file if you always want to load it by default:

    %install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
    %load_ext autotime
    

    If loaded, every output of subsequent cell execution will include the time in min and sec it took to execute it.

    0 讨论(0)
  • 2020-11-29 15:46

    If you want to print wall cell execution time here is a trick, use

    %%time
    <--code goes here-->
    

    but here make sure that, the %%time is a magic function, so put it at first line in your code.

    if you put it after some line of your code it's going to give you usage error and not gonna work.

    0 讨论(0)
  • 2020-11-29 15:48
    import time
    start = time.time()
    "the code you want to test stays here"
    end = time.time()
    print(end - start)
    
    0 讨论(0)
  • 2020-11-29 15:49

    You can use timeit magic function for that.

    %timeit CODE_LINE
    

    Or on the cell

    %%timeit 
    
    SOME_CELL_CODE
    

    Check more IPython magic functions at https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb

    0 讨论(0)
  • 2020-11-29 15:51

    The only way I found to overcome this problem is by executing the last statement with print.

    Do not forget that cell magic starts with %% and line magic starts with %.

    %%time
    clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
    res = clf.predict(X_test)
    print(res)
    

    Notice that any changes performed inside the cell are not taken into consideration in the next cells, something that is counter intuitive when there is a pipeline:

    0 讨论(0)
提交回复
热议问题