##// END OF EJS Templates
copy timeit.Timer.timeit from CPython 3.4...
Min RK -
Show More
@@ -1,25 +1,19 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Implementation of execution-related magic functions.
2 """Implementation of execution-related magic functions."""
3 """
4 from __future__ import print_function
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2012 The IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
3
13 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
14 # Imports
5 # Distributed under the terms of the Modified BSD License.
15 #-----------------------------------------------------------------------------
6
7 from __future__ import print_function
16
8
17 # Stdlib
18 import ast
9 import ast
19 import bdb
10 import bdb
11 import gc
12 import itertools
20 import os
13 import os
21 import sys
14 import sys
22 import time
15 import time
16 import timeit
23 from pdb import Restart
17 from pdb import Restart
24
18
25 # cProfile was added in Python2.5
19 # cProfile was added in Python2.5
@@ -33,7 +27,6 b' except ImportError:'
33 except ImportError:
27 except ImportError:
34 profile = pstats = None
28 profile = pstats = None
35
29
36 # Our own packages
37 from IPython.core import debugger, oinspect
30 from IPython.core import debugger, oinspect
38 from IPython.core import magic_arguments
31 from IPython.core import magic_arguments
39 from IPython.core import page
32 from IPython.core import page
@@ -115,6 +108,34 b' class TimeitTemplateFiller(ast.NodeTransformer):'
115 return node
108 return node
116
109
117
110
111 class Timer(timeit.Timer):
112 """Timer class that explicitly uses self.inner
113
114 which is an undocumented implementation detail of CPython,
115 not shared by PyPy.
116 """
117 # Timer.timeit copied from CPython 3.4.2
118 def timeit(self, number=timeit.default_number):
119 """Time 'number' executions of the main statement.
120
121 To be precise, this executes the setup statement once, and
122 then returns the time it takes to execute the main statement
123 a number of times, as a float measured in seconds. The
124 argument is the number of times through the loop, defaulting
125 to one million. The main statement, the setup statement and
126 the timer function to be used are passed to the constructor.
127 """
128 it = itertools.repeat(None, number)
129 gcold = gc.isenabled()
130 gc.disable()
131 try:
132 timing = self.inner(it, self.timer)
133 finally:
134 if gcold:
135 gc.enable()
136 return timing
137
138
118 @magics_class
139 @magics_class
119 class ExecutionMagics(Magics):
140 class ExecutionMagics(Magics):
120 """Magics related to code execution, debugging, profiling, etc.
141 """Magics related to code execution, debugging, profiling, etc.
@@ -945,8 +966,6 b' python-profiler package from non-free.""")'
945 does not matter as long as results from timeit.py are not mixed with
966 does not matter as long as results from timeit.py are not mixed with
946 those from %timeit."""
967 those from %timeit."""
947
968
948 import timeit
949
950 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
969 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
951 posix=False, strict=False)
970 posix=False, strict=False)
952 if stmt == "" and cell is None:
971 if stmt == "" and cell is None:
@@ -963,7 +982,7 b' python-profiler package from non-free.""")'
963 if hasattr(opts, "c"):
982 if hasattr(opts, "c"):
964 timefunc = clock
983 timefunc = clock
965
984
966 timer = timeit.Timer(timer=timefunc)
985 timer = Timer(timer=timefunc)
967 # this code has tight coupling to the inner workings of timeit.Timer,
986 # this code has tight coupling to the inner workings of timeit.Timer,
968 # but is there a better way to achieve that the code stmt has access
987 # but is there a better way to achieve that the code stmt has access
969 # to the shell namespace?
988 # to the shell namespace?
General Comments 0
You need to be logged in to leave comments. Login now