Show More
@@ -1,25 +1,19 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
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 | #----------------------------------------------------------------------------- | |
|
2 | """Implementation of execution-related magic functions.""" | |
|
12 | 3 | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) IPython Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | ||
|
7 | from __future__ import print_function | |
|
16 | 8 | |
|
17 | # Stdlib | |
|
18 | 9 | import ast |
|
19 | 10 | import bdb |
|
11 | import gc | |
|
12 | import itertools | |
|
20 | 13 | import os |
|
21 | 14 | import sys |
|
22 | 15 | import time |
|
16 | import timeit | |
|
23 | 17 | from pdb import Restart |
|
24 | 18 | |
|
25 | 19 | # cProfile was added in Python2.5 |
@@ -33,7 +27,6 b' except ImportError:' | |||
|
33 | 27 | except ImportError: |
|
34 | 28 | profile = pstats = None |
|
35 | 29 | |
|
36 | # Our own packages | |
|
37 | 30 | from IPython.core import debugger, oinspect |
|
38 | 31 | from IPython.core import magic_arguments |
|
39 | 32 | from IPython.core import page |
@@ -115,6 +108,34 b' class TimeitTemplateFiller(ast.NodeTransformer):' | |||
|
115 | 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 | 139 | @magics_class |
|
119 | 140 | class ExecutionMagics(Magics): |
|
120 | 141 | """Magics related to code execution, debugging, profiling, etc. |
@@ -945,8 +966,6 b' python-profiler package from non-free.""")' | |||
|
945 | 966 | does not matter as long as results from timeit.py are not mixed with |
|
946 | 967 | those from %timeit.""" |
|
947 | 968 | |
|
948 | import timeit | |
|
949 | ||
|
950 | 969 | opts, stmt = self.parse_options(line,'n:r:tcp:qo', |
|
951 | 970 | posix=False, strict=False) |
|
952 | 971 | if stmt == "" and cell is None: |
@@ -963,7 +982,7 b' python-profiler package from non-free.""")' | |||
|
963 | 982 | if hasattr(opts, "c"): |
|
964 | 983 | timefunc = clock |
|
965 | 984 | |
|
966 |
timer = |
|
|
985 | timer = Timer(timer=timefunc) | |
|
967 | 986 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
968 | 987 | # but is there a better way to achieve that the code stmt has access |
|
969 | 988 | # to the shell namespace? |
General Comments 0
You need to be logged in to leave comments.
Login now