performance - Why is Python 3 is considerably slower than Python 2? -


this question has answer here:

i've been trying understand why python 3 taking time compared python 2 in situations, below few cases i've verified python 3.4 python 2.7.

note: i've gone through of questions why there no xrange function in python3? , loop in python3 slower python2 , same code slower in python3 compared python2, feel didn't actual reason behind issue.

i've tried piece of code show how making difference:

max_num = 3*10**7  # make compatible py3.4. try:     xrange except:     xrange = range   def foo():     = max_num     while i> 0:         -= 1  def foo_for():     in xrange(max_num):         pass 

when i've tried running programme py3.4 , py2.7 i've got below results.

note: these stats came through 64 bit machine 2.6ghz processor , calculated time using time.time() in single loop.

output : python 3.4 ----------------- 2.6392083168029785 0.9724123477935791  output: python 2.7 ------------------ 1.5131521225 0.475143909454 

i don't think there has been changes applied while or xrange 2.7 3.4, know range has been started acting xrange in py3.4 documentation says

range() behaves xrange() used behave, except works values of arbitrary size. latter no longer exists.

this means change xrange range equal name change working arbitrary values.

i've verified disassembled byte code well.

below disassembled byte code function foo():

python 3.4: ---------------    13           0 load_global              0 (max_num)               3 store_fast               0 (i)   14           6 setup_loop              26 (to 35)         >>    9 load_fast                0 (i)              12 load_const               1 (0)              15 compare_op               4 (>)              18 pop_jump_if_false       34   15          21 load_fast                0 (i)              24 load_const               2 (1)              27 inplace_subtract              28 store_fast               0 (i)              31 jump_absolute            9         >>   34 pop_block         >>   35 load_const               0 (none)              38 return_value  python 2.7 -------------   13           0 load_global              0 (max_num)               3 store_fast               0 (i)   14           6 setup_loop              26 (to 35)         >>    9 load_fast                0 (i)              12 load_const               1 (0)              15 compare_op               4 (>)              18 pop_jump_if_false       34   15          21 load_fast                0 (i)              24 load_const               2 (1)              27 inplace_subtract                  28 store_fast               0 (i)              31 jump_absolute            9         >>   34 pop_block                    >>   35 load_const               0 (none)              38 return_value         

and below disassembled byte code function foo_for():

python: 3.4   19           0 setup_loop              20 (to 23)               3 load_global              0 (xrange)               6 load_global              1 (max_num)               9 call_function            1 (1 positional, 0 keyword pair)              12 get_iter         >>   13 for_iter                 6 (to 22)              16 store_fast               0 (i)   20          19 jump_absolute           13         >>   22 pop_block         >>   23 load_const               0 (none)              26 return_value   python: 2.7 -------------   19           0 setup_loop              20 (to 23)               3 load_global              0 (xrange)               6 load_global              1 (max_num)               9 call_function            1              12 get_iter                     >>   13 for_iter                 6 (to 22)              16 store_fast               0 (i)   20          19 jump_absolute           13         >>   22 pop_block                    >>   23 load_const               0 (none)              26 return_value         

if compare both byte codes they've produced same disassembled byte code.

now i'm wondering change 2.7 3.4 causing huge change in execution time in given piece of code.

the difference in implementation of int type. python 3.x uses arbitrary-sized integer type (long in 2.x) exclusively, while in python 2.x values sys.maxint simpler int type used uses simple c long under hood.

once limit loops long integers, python 3.x faster:

>>> timeit import timeit >>> max_num = 3*10**3 >>> def bar(): ...     = max_num + sys.maxsize ...     while > sys.maxsize: ...         -= 1 ...  

python 2:

>>> timeit(bar, number=10000) 5.704327821731567 

python 3:

>>> timeit(bar, number=10000) 3.7299320790334605 

i used sys.maxsize sys.maxint dropped python 3, integer value same.

the speed difference in python 2 limited first (2 ** 63) - 1 integers on 64-bit, (2 ** 31) - 1 integers on 32 bit systems.

since cannot use long type xrange() on python 2, did not include comparison function.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -