42**1000000 is CPU time free

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Jul 24 17:14:50 EDT 2015


On 24/07/2015 21:54, candide 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!!)
>

I suggest that you use the dis module to compare the code generated for 
the snippet using 'N' and that using the constant 1000000.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list