Repository: ipython/ipython

Issue:
Timeit results does not show -r value
```
In [1]: timeit -o -r7 -n11 1+1
11 loops, average of 7: 33.1 ns +- 8.77 ns per loop (using standard deviation)
Out[1]: <TimeitResult : 11 loops, average of 7: 33.1 ns +- 8.77 ns per loop (using standard deviation)>
```

Should show r=7. 


Discussion:
The format is also confusing (see proposed format [here](https://github.com/ipython/ipython/issues/9643#issuecomment-282504120), and it shoudl get a note into the what's new. 
How should this looks like? Its already shows `average of`. Is this a question of phrasing?
Sorry if I misexpressed myself. There are 2 things here r, and n. And the
way timeit works is to for example run log(x) n=1000000 times, mesure the
time and do this r=10 times.

The result array is r long, and the average/std is over the r
measurements.  N is shown but not R.

Does that make sense ?

On Mar 7, 2017 05:19, "Slam" <notifications@github.com> wrote:

> How should this looks like? Its already shows average of. Is this a
> question of phrasing?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/ipython/ipython/issues/10359#issuecomment-284718810>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAUez-1lYdGFKo1eekKJ3wmZ1XuaaKhwks5rjVlkgaJpZM4MPBhR>
> .
>

`Out[1]: <TimeitResult : 11 loops, average of 7: 33.1 ns +- 8.77 ns per loop (using standard deviation)>`

I also couldn't understand the issue. It does show both r=7 (`average of 7`) and n=11 (`11 loops`). 
> I also couldn't understand the issue. It does show both r=7 (average of 7) and n=11 (11 loops).

Ah, yes, you are right, then the error message is confusing, this reads to me as "I took 7 of the 11 loops and averaged" which is not right. Though you are right it shows both. 

Resolving patch:
diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py
--- a/IPython/core/magics/execution.py
+++ b/IPython/core/magics/execution.py
@@ -85,14 +85,28 @@ def stdev(self):
         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'<TimeitResult : '+unic+u'>')
+        unic = self.__str__()
+        p.text(u'<TimeitResult : '+unic+u'>')
 
 
 
@@ -959,20 +973,18 @@ def timeit(self, line='', cell=None):
         ::
 
           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


Test patch:
diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py
--- a/IPython/core/tests/test_interactiveshell.py
+++ b/IPython/core/tests/test_interactiveshell.py
@@ -598,12 +598,12 @@ def f(x):
             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 @@ def f(x):
             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,)})