##// END OF EJS Templates
Feature req #10556: %timeit could use local scope
adityausathe -
Show More
@@ -940,7 +940,8 b' python-profiler package from non-free.""")'
940 940
941 941 @skip_doctest
942 942 @line_cell_magic
943 def timeit(self, line='', cell=None):
943 @needs_local_scope
944 def timeit(self, line='', cell=None, local_ns=None):
944 945 """Time execution of a Python statement or expression
945 946
946 947 Usage, in line mode:
@@ -1074,7 +1075,16 b' python-profiler package from non-free.""")'
1074 1075 tc = clock()-t0
1075 1076
1076 1077 ns = {}
1077 exec(code, self.shell.user_ns, ns)
1078 glob = self.shell.user_ns
1079 # handles global vars with same name as local vars. We store them in conflict_globs.
1080 if local_ns is not None:
1081 conflict_globs = {}
1082 for var_name, var_val in glob.items():
1083 if var_name in local_ns:
1084 conflict_globs[var_name] = var_val
1085 glob.update(local_ns)
1086
1087 exec(code, glob, ns)
1078 1088 timer.inner = ns["inner"]
1079 1089
1080 1090 # This is used to check if there is a huge difference between the
@@ -1093,6 +1103,11 b' python-profiler package from non-free.""")'
1093 1103 worst = max(all_runs) / number
1094 1104 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1095 1105
1106 # Restore global vars from conflict_globs
1107 if local_ns is not None:
1108 if len(conflict_globs) > 0:
1109 glob.update(conflict_globs)
1110
1096 1111 if not quiet :
1097 1112 # Check best timing is greater than zero to avoid a
1098 1113 # ZeroDivisionError.
General Comments 0
You need to be logged in to leave comments. Login now