Simple way to measure cell execution time in ipython notebook

后端 未结 12 1420
春和景丽
春和景丽 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 :4()
        5    0.064    0.013    0.064    0.013 {built-in method sum}
        1    0.036    0.036    0.699    0.699 :1(sum_of_lists)
        1    0.014    0.014    0.714    0.714 :1()
        1    0.000    0.000    0.714    0.714 {built-in method exec}
    

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

提交回复
热议问题