##// END OF EJS Templates
Implement %%timeit as a cell level magic.
Fernando Perez -
Show More
@@ -36,8 +36,8 b' from IPython.core import debugger, oinspect'
36 from IPython.core import page
36 from IPython.core import page
37 from IPython.core.error import UsageError
37 from IPython.core.error import UsageError
38 from IPython.core.macro import Macro
38 from IPython.core.macro import Macro
39 from IPython.core.magic import (Magics, magics_class, line_magic,
39 from IPython.core.magic import (Magics, magics_class, line_magic,
40 on_off, needs_local_scope)
40 line_cell_magic, on_off, needs_local_scope)
41 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.testing.skipdoctest import skip_doctest
42 from IPython.utils import py3compat
42 from IPython.utils import py3compat
43 from IPython.utils.ipstruct import Struct
43 from IPython.utils.ipstruct import Struct
@@ -641,15 +641,26 b' python-profiler package from non-free.""")'
641 return stats
641 return stats
642
642
643 @skip_doctest
643 @skip_doctest
644 @line_magic
644 @line_cell_magic
645 def timeit(self, parameter_s=''):
645 def timeit(self, line='', cell=None):
646 """Time execution of a Python statement or expression
646 """Time execution of a Python statement or expression
647
647
648 Usage:\\
648 Usage, in line mode:
649 %timeit [-n<N> -r<R> [-t|-c]] statement
649 %timeit [-n<N> -r<R> [-t|-c]] statement
650 or in cell mode:
651 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
652 code
653 code...
650
654
651 Time execution of a Python statement or expression using the timeit
655 Time execution of a Python statement or expression using the timeit
652 module.
656 module. This function can be used both as a line and cell magic:
657
658 - In line mode you can time a single-line statement (though multiple
659 ones can be chained with using semicolons).
660
661 - In cell mode, the statement in the first line is used as setup code
662 (executed but not timed) and the body of the cell is timed. The cell
663 body has access to any variables created in the setup code.
653
664
654 Options:
665 Options:
655 -n<N>: execute the given statement <N> times in a loop. If this value
666 -n<N>: execute the given statement <N> times in a loop. If this value
@@ -725,7 +736,7 b' python-profiler package from non-free.""")'
725
736
726 scaling = [1, 1e3, 1e6, 1e9]
737 scaling = [1, 1e3, 1e6, 1e9]
727
738
728 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
739 opts, stmt = self.parse_options(line,'n:r:tcp:',
729 posix=False, strict=False)
740 posix=False, strict=False)
730 if stmt == "":
741 if stmt == "":
731 return
742 return
@@ -743,8 +754,15 b' python-profiler package from non-free.""")'
743 # but is there a better way to achieve that the code stmt has access
754 # but is there a better way to achieve that the code stmt has access
744 # to the shell namespace?
755 # to the shell namespace?
745
756
746 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
757 if cell is None:
747 'setup': "pass"}
758 # called as line magic
759 setup = 'pass'
760 stmt = timeit.reindent(stmt, 8)
761 else:
762 setup = timeit.reindent(stmt, 4)
763 stmt = timeit.reindent(cell, 8)
764
765 src = timeit.template % dict(stmt=stmt, setup=setup)
748 # Track compilation time so it can be reported if too long
766 # Track compilation time so it can be reported if too long
749 # Minimum time above which compilation time will be reported
767 # Minimum time above which compilation time will be reported
750 tc_min = 0.1
768 tc_min = 0.1
General Comments 0
You need to be logged in to leave comments. Login now