42**1000000 is CPU time free

Chris Kaynor ckaynor at zindagigames.com
Fri Jul 24 17:08:56 EDT 2015


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.

Basically, what you really wrote in your code is:

import time
a = time.clock()
42000000000000000000000000000000000000000...0000000000 # Replace the ...
with zeros until you have the actual value.
b = time.clock()


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).



The first example can measure the time as you are using a variable, which
bypasses Python's literal optimization.

Chris

On Fri, Jul 24, 2015 at 1:54 PM, candide <c.candide at laposte.net> wrote:

> Of course, computing 42**1000000 is not free:
>
>
> # ------------------
> import time
>
> a=time.clock()
>
> N=1000000
> 42**N
>
> b=time.clock()
>
> print("CPU TIME :", b - a)
> # ------------------
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> CPU TIME : 2.37
>
> real    0m2.412s
> user    0m2.388s
> sys     0m0.016s
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> So please, explain the following:
>
>
> # ------------------
> import time
>
> a=time.clock()
>
> 42**1000000
>
> b=time.clock()
>
> print("CPU TIME :", b - a)
> # ------------------
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> CPU TIME : 0.0
>
> real    0m2.410s
> user    0m2.400s
> sys     0m0.008s
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> (focus on the CPU TIME!!)
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150724/840e9f70/attachment.html>


More information about the Python-list mailing list