On Sat, 25 Jul 2015 07:35 am, candide wrote:
> Thanks to all for your response, I was not aware that the interpreter
> evaluated pure litteral expressions at compile time.
This is an implementation-dependent optimization, so different versions of
Python may do more, or less, or even no, optimiza
Thanks to all for your response, I was not aware that the interpreter evaluated
pure litteral expressions at compile time.
--
https://mail.python.org/mailman/listinfo/python-list
On Friday 24 Jul 2015 22:54 CEST, candide wrote:
> Of course, computing 42**100 is not free:
>
>
> # --
> import time
>
> a=time.clock()
>
> N=100
> 42**N
>
> b=time.clock()
>
> print("CPU TIME :", b - a)
> # --
>
>
> ~~
> CPU TIM
On 24/07/2015 21:54, candide wrote:
Of course, computing 42**100 is not free:
# --
import time
a=time.clock()
N=100
42**N
b=time.clock()
print("CPU TIME :", b - a)
# --
~~
CPU TIME : 2.37
real0m2.412s
user0m2.388
On Fri, Jul 24, 2015 at 3:54 PM, candide wrote:
> Of course, computing 42**100 is not free:
> So please, explain the following:
>
> (focus on the CPU TIME!!)
In your second example, the peephole optimizer gets hold of it and
does the calculation at compile time:
Python 3.4.3 (v3.4.3:9b73f1c
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()
42000...00 # Replace the ...
with z
Of course, computing 42**100 is not free:
# --
import time
a=time.clock()
N=100
42**N
b=time.clock()
print("CPU TIME :", b - a)
# --
~~
CPU TIME : 2.37
real0m2.412s
user0m2.388s
sys 0m0.016s
~~~