From 6bdf956e79a04155ea487f78e10cbd66651c1265 2017-03-20 22:18:09 From: Matthias Bussonnier Date: 2017-03-20 22:18:09 Subject: [PATCH] Improve timeit message to fix ambuiguity. 10 loops, best of 7 is a bit ambiguous, be clearer about what is meant. Closes #10359 --- diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index ff330f1..d9d77dc 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -85,14 +85,20 @@ class TimeitResult(object): return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5 def __str__(self): - return (u"%s loop%s, average of %d: %s +- %s per loop (using standard deviation)" - % (self.loops,"" if self.loops == 1 else "s", self.repeat, - _format_time(self.average, self._precision), - _format_time(self.stdev, self._precision))) + return ( + u"{mean} ± {std} per loop (mean ± std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)" + .format( + runs = self.repeat, + loops = self.loops, + loop_plural = "" if self.loops == 1 else "s", + run_plural = "" if self.repeat == 1 else "s", + mean = _format_time(self.average, self._precision), + std = _format_time(self.stdev, self._precision)) + ) def _repr_pretty_(self, p , cycle): - unic = self.__str__() - p.text(u'') + unic = self.__str__() + p.text(u'') @@ -959,20 +965,18 @@ python-profiler package from non-free.""") :: In [1]: %timeit pass - 100000000 loops, average of 7: 5.48 ns +- 0.354 ns per loop (using standard deviation) + 8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) In [2]: u = None In [3]: %timeit u is None - 10000000 loops, average of 7: 22.7 ns +- 2.33 ns per loop (using standard deviation) + 29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [4]: %timeit -r 4 u == None - 10000000 loops, average of 4: 27.5 ns +- 2.91 ns per loop (using standard deviation) In [5]: import time In [6]: %timeit -n1 time.sleep(2) - 1 loop, average of 7: 2 s +- 4.71 µs per loop (using standard deviation) The times reported by %timeit will be slightly higher than those diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 37b7a5b..a7402d9 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -598,12 +598,12 @@ class TestAstTransform(unittest.TestCase): called.add(x) ip.push({'f':f}) - with tt.AssertPrints("average of "): + with tt.AssertPrints("mean ± std"): ip.run_line_magic("timeit", "-n1 f(1)") self.assertEqual(called, {-1}) called.clear() - with tt.AssertPrints("average of "): + with tt.AssertPrints("mean ± std"): ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)") self.assertEqual(called, {-2, -3}) @@ -671,12 +671,12 @@ class TestAstTransform2(unittest.TestCase): called.add(x) ip.push({'f':f}) - with tt.AssertPrints("average of "): + with tt.AssertPrints("mean ± std"): ip.run_line_magic("timeit", "-n1 f(1)") self.assertEqual(called, {(1,)}) called.clear() - with tt.AssertPrints("average of "): + with tt.AssertPrints("mean ± std"): ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)") self.assertEqual(called, {(2,), (3,)})