Use time.time () because the timer ended with the error `**`

I'm a Python 3 student, and lately I have been confused by the strange time.time () behavior.

I wrote two pieces of code and timed them time.time ():

pow version

from time import time

t0 = time()
x = pow(2, 1000000000)
t1 = time()
print(t1 - t0)

pow picture


** version

from time import time

t0 = time()
x = 2 ** 1000000000
t1 = time()
print(t1 - t0)

** picture


pow: time_cost = 1.544sec, output = 1.43625617027

**: time_cost = 1.526sec, output = 0.0

Why time.time () does not work for version **


Additional Information:

sys.version = '3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]',

sys.winver = '3.6-32',

sys.platform = 'win32',

sys.implementation = namespace(cache_tag='cpython-36', hexversion=50725616, name='cpython', version=sys.version_info(major=3, minor=6, micro=2, releaselevel='final', serial=0))

+4
source share
1 answer

This is because Python 3 handles certain constant expressions when processing a file. If you run the program:

print("start")
x = 2**1000000000
print("end")

, , "start" "end" . :

print("start")
x = pow(2, 1000000000)
print("end")

"start", "".

Python " " 2**1000000000, ( ), . , pow(2,1000000000) ; , .

. :

# starstar.py
x = 2**1000000000

# pow.py
x = pow(2,1000000000)

:

# main.py
import starstar
import pow

python main.py, Python (, __pycache__ - ). , - starstar.pyc - 2**1000000000; pow.pyc .

+3

Source: https://habr.com/ru/post/1684966/


All Articles