<div dir="ltr">As you are doing an operation on a literal, Python is computing the value at import time, which occurs before your time.clock() calls run.<div><br></div><div>Basically, what you really wrote in your code is:</div><div><br></div><div>import time</div><div>a = time.clock()</div><div>42000000000000000000000000000000000000000...0000000000 # Replace the ... with zeros until you have the actual value.</div><div>b = time.clock()</div><div><br></div><div><br></div><div>The computation time is still being shown in the real and user times reported by the external call. Similarly, if you were to put time.clock() calls around the import statement, you would see the time there, for the first import statement (Python caches imports, so they generally only run once per instance).</div><div><br></div><div><br></div><div><br></div><div>The first example can measure the time as you are using a variable, which bypasses Python's literal optimization.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">Chris</div></div>
<br><div class="gmail_quote">On Fri, Jul 24, 2015 at 1:54 PM, candide <span dir="ltr"><<a href="mailto:c.candide@laposte.net" target="_blank">c.candide@laposte.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Of course, computing 42**1000000 is not free:<br>
<br>
<br>
# ------------------<br>
import time<br>
<br>
a=time.clock()<br>
<br>
N=1000000<br>
42**N<br>
<br>
b=time.clock()<br>
<br>
print("CPU TIME :", b - a)<br>
# ------------------<br>
<br>
<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
CPU TIME : 2.37<br>
<br>
real    0m2.412s<br>
user    0m2.388s<br>
sys     0m0.016s<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
<br>
So please, explain the following:<br>
<br>
<br>
# ------------------<br>
import time<br>
<br>
a=time.clock()<br>
<br>
42**1000000<br>
<br>
b=time.clock()<br>
<br>
print("CPU TIME :", b - a)<br>
# ------------------<br>
<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
CPU TIME : 0.0<br>
<br>
real    0m2.410s<br>
user    0m2.400s<br>
sys     0m0.008s<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
<br>
(focus on the CPU TIME!!)<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>