class_docstr: Magics related to code execution, debugging, profiling, etc.
  
      
docstr: Time execution of a Python statement or expression
  
  Usage, in line mode:
    %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
  or in cell mode:
    %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
    code
    code...
  
  Time execution of a Python statement or expression using the timeit
  module.  This function can be used both as a line and cell magic:
  
  - In line mode you can time a single-line statement (though multiple
    ones can be chained with using semicolons).
  
  - In cell mode, the statement in the first line is used as setup code
    (executed but not timed) and the body of the cell is timed.  The cell
    body has access to any variables created in the setup code.
  
  Options:
  -n<N>: execute the given statement <N> times in a loop. If this value
  is not given, a fitting value is chosen.
  
  -r<R>: repeat the loop iteration <R> times and take the best result.
  Default: 3
  
  -t: use time.time to measure the time, which is the default on Unix.
  This function measures wall time.
  
  -c: use time.clock to measure the time, which is the default on
  Windows and measures wall time. On Unix, resource.getrusage is used
  instead and returns the CPU user time.
  
  -p<P>: use a precision of <P> digits to display the timing result.
  Default: 3
  
  -q: Quiet, do not print result.
  
  -o: return a TimeitResult that can be stored in a variable to inspect
      the result in more details.
  
  
  Examples
  --------
  ::
  
    In [1]: %timeit pass
    10000000 loops, best of 3: 53.3 ns per loop
  
    In [2]: u = None
  
    In [3]: %timeit u is None
    10000000 loops, best of 3: 184 ns per loop
  
    In [4]: %timeit -r 4 u == None
    1000000 loops, best of 4: 242 ns per loop
  
    In [5]: import time
  
    In [6]: %timeit -n1 time.sleep(2)
    1 loop, best of 3: 2 s per loop
  
  
  The times reported by %timeit will be slightly higher than those
  reported by the timeit.py script when variables are accessed. This is
  due to the fact that %timeit executes the statement in the namespace
  of the shell, compared with timeit.py, which uses a single setup
  statement to import function or create variables. Generally, the bias
  does not matter as long as results from timeit.py are not mixed with
  those from %timeit.
docstr_tok: ["Time","execution","of","a","Python","statement","or","expression","Usage",",","in","line","mode",":","%","timeit","[","-n","<","N",">","-r","<","R",">","[","-t|-c","]","-q","-p","<","P",">","-o","]","statement","or","in","cell","mode",":","%","%","timeit","[","-n","<","N",">","-r","<","R",">","[","-t|-c","]","-q","-p","<","P",">","-o","]","setup_code","code","code","...","Time","execution","of","a","Python","statement","or","expression","using","the","timeit","module",".","This","function","can","be","used","both","as","a","line","and","cell","magic",":","-","In","line","mode","you","can","time","a","single-line","statement","(","though","multiple","ones","can","be","chained","with","using","semicolons",")",".","-","In","cell","mode",",","the","statement","in","the","first","line","is","used","as","setup","code","(","executed","but","not","timed",")","and","the","body","of","the","cell","is","timed",".","The","cell","body","has","access","to","any","variables","created","in","the","setup","code",".","Options",":","-n","<","N",">",":","execute","the","given","statement","<","N",">","times","in","a","loop",".","If","this","value","is","not","given",",","a","fitting","value","is","chosen",".","-r","<","R",">",":","repeat","the","loop","iteration","<","R",">","times","and","take","the","best","result",".","Default",":","3","-t",":","use","time.time","to","measure","the","time",",","which","is","the","default","on","Unix",".","This","function","measures","wall","time",".","-c",":","use","time.clock","to","measure","the","time",",","which","is","the","default","on","Windows","and","measures","wall","time",".","On","Unix",",","resource.getrusage","is","used","instead","and","returns","the","CPU","user","time",".","-p","<","P",">",":","use","a","precision","of","<","P",">","digits","to","display","the","timing","result",".","Default",":","3","-q",":","Quiet",",","do","not","print","result",".","-o",":","return","a","TimeitResult","that","can","be","stored","in","a","variable","to","inspect","the","result","in","more","details",".","Examples","--","--","--","--",":",":","In","[","1","]",":","%","timeit","pass","10000000","loops",",","best","of","3",":","53.3","ns","per","loop","In","[","2","]",":","u","=","None","In","[","3","]",":","%","timeit","u","is","None","10000000","loops",",","best","of","3",":","184","ns","per","loop","In","[","4","]",":","%","timeit","-r","4","u","==","None","1000000","loops",",","best","of","4",":","242","ns","per","loop","In","[","5","]",":","import","time","In","[","6","]",":","%","timeit","-n1","time.sleep","(","2",")","1","loop",",","best","of","3",":","2","s","per","loop","The","times","reported","by","%","timeit","will","be","slightly","higher","than","those","reported","by","the","timeit.py","script","when","variables","are","accessed",".","This","is","due","to","the","fact","that","%","timeit","executes","the","statement","in","the","namespace","of","the","shell",",","compared","with","timeit.py",",","which","uses","a","single","setup","statement","to","import","function","or","create","variables",".","Generally",",","the","bias","does","not","matter","as","long","as","results","from","timeit.py","are","not","mixed","with","those","from","%","timeit","."]
code: def timeit(self, line="", cell=None):
      """Time execution of a Python statement or expression
  
      Usage, in line mode:
        %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
      or in cell mode:
        %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
        code
        code...
  
      Time execution of a Python statement or expression using the timeit
      module.  This function can be used both as a line and cell magic:
  
      - In line mode you can time a single-line statement (though multiple
        ones can be chained with using semicolons).
  
      - In cell mode, the statement in the first line is used as setup code
        (executed but not timed) and the body of the cell is timed.  The cell
        body has access to any variables created in the setup code.
  
      Options:
      -n<N>: execute the given statement <N> times in a loop. If this value
      is not given, a fitting value is chosen.
  
      -r<R>: repeat the loop iteration <R> times and take the best result.
      Default: 3
  
      -t: use time.time to measure the time, which is the default on Unix.
      This function measures wall time.
  
      -c: use time.clock to measure the time, which is the default on
      Windows and measures wall time. On Unix, resource.getrusage is used
      instead and returns the CPU user time.
  
      -p<P>: use a precision of <P> digits to display the timing result.
      Default: 3
  
      -q: Quiet, do not print result.
  
      -o: return a TimeitResult that can be stored in a variable to inspect
          the result in more details.
  
  
      Examples
      --------
      ::
  
        In [1]: %timeit pass
        10000000 loops, best of 3: 53.3 ns per loop
  
        In [2]: u = None
  
        In [3]: %timeit u is None
        10000000 loops, best of 3: 184 ns per loop
  
        In [4]: %timeit -r 4 u == None
        1000000 loops, best of 4: 242 ns per loop
  
        In [5]: import time
  
        In [6]: %timeit -n1 time.sleep(2)
        1 loop, best of 3: 2 s per loop
  
  
      The times reported by %timeit will be slightly higher than those
      reported by the timeit.py script when variables are accessed. This is
      due to the fact that %timeit executes the statement in the namespace
      of the shell, compared with timeit.py, which uses a single setup
      statement to import function or create variables. Generally, the bias
      does not matter as long as results from timeit.py are not mixed with
      those from %timeit."""
  
      opts, stmt = self.parse_options(
          line, "n:r:tcp:qo", posix=False, strict=False
      )
      if stmt == "" and cell is None:
          return
  
      timefunc = timeit.default_timer
      number = int(getattr(opts, "n", 0))
      repeat = int(getattr(opts, "r", timeit.default_repeat))
      precision = int(getattr(opts, "p", 3))
      quiet = "q" in opts
      return_result = "o" in opts
      if hasattr(opts, "t"):
          timefunc = time.time
      if hasattr(opts, "c"):
          timefunc = clock
  
      timer = Timer(timer=timefunc)
      # this code has tight coupling to the inner workings of timeit.Timer,
      # but is there a better way to achieve that the code stmt has access
      # to the shell namespace?
      transform = self.shell.input_splitter.transform_cell
  
      if cell is None:
          # called as line magic
          ast_setup = self.shell.compile.ast_parse("pass")
          ast_stmt = self.shell.compile.ast_parse(transform(stmt))
      else:
          ast_setup = self.shell.compile.ast_parse(transform(stmt))
          ast_stmt = self.shell.compile.ast_parse(transform(cell))
  
      ast_setup = self.shell.transform_ast(ast_setup)
      ast_stmt = self.shell.transform_ast(ast_stmt)
  
      # Check that these compile to valid Python code *outside* the timer func
      # Invalid code may become valid when put inside the function & loop,
      # which messes up error messages.
      # https://github.com/ipython/ipython/issues/10636
      self.shell.compile(ast_setup, "<magic-timeit-setup>", "exec")
      self.shell.compile(ast_stmt, "<magic-timeit-stmt>", "exec")
  
      # This codestring is taken from timeit.template - we fill it in as an
      # AST, so that we can apply our AST transformations to the user code
      # without affecting the timing code.
      timeit_ast_template = ast.parse(
          "def inner(_it, _timer):\n"
          "    setup\n"
          "    _t0 = _timer()\n"
          "    for _i in _it:\n"
          "        stmt\n"
          "    _t1 = _timer()\n"
          "    return _t1 - _t0\n"
      )
  
      timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(
          timeit_ast_template
      )
      timeit_ast = ast.fix_missing_locations(timeit_ast)
  
      # Track compilation time so it can be reported if too long
      # Minimum time above which compilation time will be reported
      tc_min = 0.1
  
      t0 = clock()
      code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
      tc = clock() - t0
  
      ns = {}
      exec(code, self.shell.user_ns, ns)
      timer.inner = ns["inner"]
  
      # This is used to check if there is a huge difference between the
      # best and worst timings.
      # Issue: https://github.com/ipython/ipython/issues/6471
      worst_tuning = 0
      if number == 0:
          # determine number so that 0.2 <= total time < 2.0
          number = 1
          for _ in range(1, 10):
              time_number = timer.timeit(number)
              worst_tuning = max(worst_tuning, time_number / number)
              if time_number >= 0.2:
                  break
              number *= 10
      all_runs = timer.repeat(repeat, number)
      best = min(all_runs) / number
  
      worst = max(all_runs) / number
      if worst_tuning:
          worst = max(worst, worst_tuning)
  
      if not quiet:
          # Check best timing is greater than zero to avoid a
          # ZeroDivisionError.
          # In cases where the slowest timing is lesser than a micosecond
          # we assume that it does not really matter if the fastest
          # timing is 4 times faster than the slowest timing or not.
          if worst > 4 * best and best > 0 and worst > 1e-6:
              print(
                  "The slowest run took %0.2f times longer than the "
                  "fastest. This could mean that an intermediate result "
                  "is being cached." % (worst / best)
              )
          if number == 1:  # No s at "loops" if only one loop
              print(
                  "%d loop, best of %d: %s per loop"
                  % (number, repeat, _format_time(best, precision))
              )
          else:
              print(
                  "%d loops, best of %d: %s per loop"
                  % (number, repeat, _format_time(best, precision))
              )
          if tc > tc_min:
              print("Compiler time: %.2f s" % tc)
      if return_result:
          return TimeitResult(
              number, repeat, best, worst, all_runs, tc, precision
          )
  
imports: from __future__ import print_function
  from __future__ import absolute_import
  import ast
  import bdb
  import gc
  import itertools
  import os
  import sys
  import time
  import timeit
  from pdb import Restart
  from IPython.core import oinspect
  from IPython.core import magic_arguments
  from IPython.core import page
  from IPython.core.error import UsageError
  from IPython.core.macro import Macro
  from IPython.core.magic import Magics, magics_class, line_magic, cell_magic, line_cell_magic, on_off, needs_local_scope
  from IPython.testing.skipdoctest import skip_doctest
  from IPython.utils import py3compat
  from IPython.utils.py3compat import builtin_mod, iteritems, PY3
  from IPython.utils.contexts import preserve_keys
  from IPython.utils.capture import capture_output
  from IPython.utils.ipstruct import Struct
  from IPython.utils.module_paths import find_mod
  from IPython.utils.path import get_py_filename, shellglob
  from IPython.utils.timing import clock, clock2
  from warnings import warn
  from logging import error
label_desc: Use image node_id 13 for calling the ExecutionMagics obj's underlying member method code with example usage: obj.timeit(line, cell) and returns: TimeitResult