diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index ff330f1..fe60e27 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -85,14 +85,28 @@ 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))) + pm = '+-' + if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: + try: + u'\xb1'.encode(sys.stdout.encoding) + pm = u'\xb1' + except: + pass + return ( + u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)" + .format( + pm = pm, + 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 +973,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..4f166f9 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("std. dev. of"): ip.run_line_magic("timeit", "-n1 f(1)") self.assertEqual(called, {-1}) called.clear() - with tt.AssertPrints("average of "): + with tt.AssertPrints("std. dev. of"): 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("std. dev. of"): ip.run_line_magic("timeit", "-n1 f(1)") self.assertEqual(called, {(1,)}) called.clear() - with tt.AssertPrints("average of "): + with tt.AssertPrints("std. dev. of"): ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)") self.assertEqual(called, {(2,), (3,)})