Simple way to measure cell execution time in ipython notebook

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

提交回复
热议问题