42**1000000 is CPU time free

Zachary Ware zachary.ware+pylist at gmail.com
Fri Jul 24 17:09:36 EDT 2015


On Fri, Jul 24, 2015 at 3:54 PM, candide <c.candide at laposte.net> wrote:
> Of course, computing 42**1000000 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:9b73f1c3e601, Feb 23 2015, 02:52:03)
 [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from dis import dis
 >>> def with_var(): N = 100; return 42**N
 ...
 >>> def no_var(): return 42**100
 ...
 >>> dis(with_var)
  1           0 LOAD_CONST               1 (100)
              3 STORE_FAST               0 (N)
              6 LOAD_CONST               2 (42)
              9 LOAD_FAST                0 (N)
             12 BINARY_POWER
             13 RETURN_VALUE
 >>> dis(no_var)
  1           0 LOAD_CONST               3
(2113143741011360736530044045523113991698878330713580061264477934391564919875497777688215057732151811172029315247932158994879668553186145824710950394684126712037376)
              3 RETURN_VALUE
 >>>


-- 
Zach



More information about the Python-list mailing list