diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 0ab0669..eaeb2fa 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1006,16 +1006,31 @@ python-profiler package from non-free.""") 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): - if timer.timeit(number) >= 0.2: + 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 if not quiet : + worst = max(all_runs) / number + if worst_tuning: + worst = max(worst, worst_tuning) + # Check best timing is greater than zero to avoid a + # ZeroDivisionError. + if worst > 4 * best and best > 0: + print("The slowest run took %0.2f times longer than the " + "fastest. This could mean that an intermediate result " + "is being cached " % (worst / best)) print(u"%d loops, best of %d: %s per loop" % (number, repeat, _format_time(best, precision))) if tc > tc_min: diff --git a/docs/source/whatsnew/pr/warning_timeit_cache.rst b/docs/source/whatsnew/pr/warning_timeit_cache.rst new file mode 100644 index 0000000..3f287e8 --- /dev/null +++ b/docs/source/whatsnew/pr/warning_timeit_cache.rst @@ -0,0 +1,3 @@ +Using %timeit prints warnings if there is atleast a 4x difference in timings +between the slowest and fastest runs, since this might meant that the multiple +runs are not independent of one another. \ No newline at end of file