Re: Way to retrieve the value of %time?

Matthias Bussonnier <[email protected]> Thu, 16 Jul 2015 11:28:29 -0700
Newsgroups gmane.comp.python.ipython.user
Message-ID <[email protected]>
--===============1243656665==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_EC37A3B6-648E-425B-BCE6-1D92261C92AE"


--Apple-Mail=_EC37A3B6-648E-425B-BCE6-1D92261C92AE
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi,=20

I would suggest asking on [email protected] =
<mailto:[email protected]>, IPython-user is deprecated.=20
Where did you find the link to this ML ?


for your answer, use %timeit (not %time), with the -o options.=20
--=20
M=20


Docstring:
Time execution of a Python statement or expression

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.

Options:
-n<N>: execute the given statement <N> times in a loop. If this value
is not given, a fitting value is chosen.

-r<R>: repeat the loop iteration <R> times and take the best result.
Default: 3

-t: use time.time to measure the time, which is the default on Unix.
This function measures wall time.

-c: use time.clock to measure the time, which is the default on
Windows and measures wall time. On Unix, resource.getrusage is used
instead and returns the CPU user time.

-p<P>: use a precision of <P> digits to display the timing result.
Default: 3

-q: Quiet, do not print result.

-o: return a TimeitResult that can be stored in a variable to inspect
    the result in more details.


Examples
--------
::

  In [1]: %timeit pass
  10000000 loops, best of 3: 53.3 ns per loop

  In [2]: u =3D None

  In [3]: %timeit u is None
  10000000 loops, best of 3: 184 ns per loop

  In [4]: %timeit -r 4 u =3D=3D None
  1000000 loops, best of 4: 242 ns per loop

  In [5]: import time

  In [6]: %timeit -n1 time.sleep(2)
  1 loops, best of 3: 2 s per loop


The times reported by %timeit will be slightly higher than those
reported by the timeit.py script when variables are accessed. This is
due to the fact that %timeit executes the statement in the namespace
of the shell, compared with timeit.py, which uses a single setup
statement to import function or create variables. Generally, the bias
does not matter as long as results from timeit.py are not mixed with
those from %timeit.




> On Jul 16, 2015, at 11:22, Francesc Alted <[email protected]> wrote:
>=20
> Hi,
>=20
> I am trying to come up with a way of accessing the time measured by =
the magic %time.  That is, in:
>=20
> In [149]: %time sum(i for i in xrange(1000000))
> CPU times: user 118 ms, sys: 27.4 ms, total: 145 ms
> Wall time: 121 ms
> Out[149]: 499999500000
>=20
> is there a way to get the 121 ms value of the 'Wall time' above and =
put that in a variable?  I know I can use something like:
>=20
> In [150]: from time import time
>=20
> In [151]: t0 =3D time(); sum(i for i in xrange(1000000)); mytime =3D =
time() - t0
>=20
> In [152]: mytime
> Out[152]: 0.10076594352722168
>=20
> but %time is so much convenient, specially for tutorials, that I would =
rather prefer using it.
>=20
> Thanks!
>=20
> --=20
> Francesc Alted
> _______________________________________________
> IPython-User mailing list
> [email protected]
> http://mail.scipy.org/mailman/listinfo/ipython-user


--Apple-Mail=_EC37A3B6-648E-425B-BCE6-1D92261C92AE
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D"">Hi,&nbsp;<div class=3D""><br class=3D""></div><div class=3D"">I=
 would suggest asking on <a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>, IPython-user is =
deprecated.&nbsp;</div><div class=3D"">Where did you find the link to =
this ML ?</div><div class=3D""><br class=3D""></div><div class=3D""><br =
class=3D""></div><div class=3D"">for your answer, use %timeit (not =
%time), with the -o options.&nbsp;</div><div class=3D"">--&nbsp;</div><div=
 class=3D"">M&nbsp;</div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D""><div =
class=3D"">Docstring:</div><div class=3D"">Time execution of a Python =
statement or expression</div><div class=3D""><br class=3D""></div><div =
class=3D"">Usage, in line mode:</div><div class=3D"">&nbsp; %timeit =
[-n&lt;N&gt; -r&lt;R&gt; [-t|-c] -q -p&lt;P&gt; -o] statement</div><div =
class=3D"">or in cell mode:</div><div class=3D"">&nbsp; %%timeit =
[-n&lt;N&gt; -r&lt;R&gt; [-t|-c] -q -p&lt;P&gt; -o] setup_code</div><div =
class=3D"">&nbsp; code</div><div class=3D"">&nbsp; code...</div><div =
class=3D""><br class=3D""></div><div class=3D"">Time execution of a =
Python statement or expression using the timeit</div><div =
class=3D"">module. &nbsp;This function can be used both as a line and =
cell magic:</div><div class=3D""><br class=3D""></div><div class=3D"">- =
In line mode you can time a single-line statement (though =
multiple</div><div class=3D"">&nbsp; ones can be chained with using =
semicolons).</div><div class=3D""><br class=3D""></div><div class=3D"">- =
In cell mode, the statement in the first line is used as setup =
code</div><div class=3D"">&nbsp; (executed but not timed) and the body =
of the cell is timed. &nbsp;The cell</div><div class=3D"">&nbsp; body =
has access to any variables created in the setup code.</div><div =
class=3D""><br class=3D""></div><div class=3D"">Options:</div><div =
class=3D"">-n&lt;N&gt;: execute the given statement &lt;N&gt; times in a =
loop. If this value</div><div class=3D"">is not given, a fitting value =
is chosen.</div><div class=3D""><br class=3D""></div><div =
class=3D"">-r&lt;R&gt;: repeat the loop iteration &lt;R&gt; times and =
take the best result.</div><div class=3D"">Default: 3</div><div =
class=3D""><br class=3D""></div><div class=3D"">-t: use time.time to =
measure the time, which is the default on Unix.</div><div class=3D"">This =
function measures wall time.</div><div class=3D""><br =
class=3D""></div><div class=3D"">-c: use time.clock to measure the time, =
which is the default on</div><div class=3D"">Windows and measures wall =
time. On Unix, resource.getrusage is used</div><div class=3D"">instead =
and returns the CPU user time.</div><div class=3D""><br =
class=3D""></div><div class=3D"">-p&lt;P&gt;: use a precision of =
&lt;P&gt; digits to display the timing result.</div><div =
class=3D"">Default: 3</div><div class=3D""><br class=3D""></div><div =
class=3D"">-q: Quiet, do not print result.</div><div class=3D""><br =
class=3D""></div><div class=3D"">-o: return a TimeitResult that can be =
stored in a variable to inspect</div><div class=3D"">&nbsp; &nbsp; the =
result in more details.</div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D"">Examples</div><div =
class=3D"">--------</div><div class=3D"">::</div><div class=3D""><br =
class=3D""></div><div class=3D"">&nbsp; In [1]: %timeit pass</div><div =
class=3D"">&nbsp; 10000000 loops, best of 3: 53.3 ns per loop</div><div =
class=3D""><br class=3D""></div><div class=3D"">&nbsp; In [2]: u =3D =
None</div><div class=3D""><br class=3D""></div><div class=3D"">&nbsp; In =
[3]: %timeit u is None</div><div class=3D"">&nbsp; 10000000 loops, best =
of 3: 184 ns per loop</div><div class=3D""><br class=3D""></div><div =
class=3D"">&nbsp; In [4]: %timeit -r 4 u =3D=3D None</div><div =
class=3D"">&nbsp; 1000000 loops, best of 4: 242 ns per loop</div><div =
class=3D""><br class=3D""></div><div class=3D"">&nbsp; In [5]: import =
time</div><div class=3D""><br class=3D""></div><div class=3D"">&nbsp; In =
[6]: %timeit -n1 time.sleep(2)</div><div class=3D"">&nbsp; 1 loops, best =
of 3: 2 s per loop</div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D"">The times reported by =
%timeit will be slightly higher than those</div><div class=3D"">reported =
by the timeit.py script when variables are accessed. This is</div><div =
class=3D"">due to the fact that %timeit executes the statement in the =
namespace</div><div class=3D"">of the shell, compared with timeit.py, =
which uses a single setup</div><div class=3D"">statement to import =
function or create variables. Generally, the bias</div><div =
class=3D"">does not matter as long as results from timeit.py are not =
mixed with</div><div class=3D"">those from %timeit.</div></div><div =
class=3D""><br class=3D""></div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D""><br class=3D""><div =
class=3D""><div><blockquote type=3D"cite" class=3D""><div class=3D"">On =
Jul 16, 2015, at 11:22, Francesc Alted &lt;<a =
href=3D"mailto:[email protected]" class=3D"">[email protected]</a>&gt; =
wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><div =
dir=3D"ltr" class=3D"">Hi,<div class=3D""><br class=3D""></div><div =
class=3D"">I am trying to come up with a way of accessing the time =
measured by the magic %time.&nbsp; That is, in:</div><div class=3D""><br =
class=3D""></div><div class=3D"">In [149]: %time sum(i for i in =
xrange(1000000))<div class=3D"">CPU times: user 118 ms, sys: 27.4 ms, =
total: 145 ms</div><div class=3D"">Wall time: 121 ms</div><div =
class=3D"">Out[149]: 499999500000</div><div class=3D""><br =
class=3D""></div><div class=3D"">is there a way to get the 121 ms value =
of the 'Wall time' above and put that in a variable?&nbsp; I know I can =
use something like:</div><div class=3D""><br class=3D""></div><div =
class=3D""><div class=3D"">In [150]: from time import time</div><div =
class=3D""><br class=3D""></div><div class=3D"">In [151]: t0 =3D time(); =
sum(i for i in xrange(1000000)); mytime =3D time() - t0</div><div =
class=3D""><br class=3D""></div><div class=3D"">In [152]: =
mytime</div><div class=3D"">Out[152]: =
0.10076594352722168</div></div><div class=3D""><br class=3D""></div><div =
class=3D"">but %time is so much convenient, specially for tutorials, =
that I would rather prefer using it.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Thanks!</div><div class=3D""><br =
class=3D""></div>-- <br class=3D""><div class=3D"gmail_signature">Francesc=
 Alted</div>
</div></div>
_______________________________________________<br class=3D"">IPython-User=
 mailing list<br class=3D""><a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a><br =
class=3D"">http://mail.scipy.org/mailman/listinfo/ipython-user<br =
class=3D""></div></blockquote></div><br =
class=3D""></div></div></body></html>=

--Apple-Mail=_EC37A3B6-648E-425B-BCE6-1D92261C92AE--

--===============1243656665==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
IPython-User mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/ipython-user

--===============1243656665==--