python - Why is list comprehension so faster? -


this question has answer here:

i wondering why list comprehension faster appending list. though difference wasn't expressive, is.

>>> import timeit  >>> timeit.timeit(stmt='''\ t = [] in range(10000):     t.append(i)''', number=10000) 9.467898777974142  >>> timeit.timeit(stmt='t= [i in range(10000)]', number=10000) 4.1138417314859 

the difference list comprehension 50% faster. why?

list comprehensions perform better here because don’t need load append attribute of list , call function!*

consider following example :

>>> import dis >>> def f1(): ...   in range(5): ...      l.append(i) ...  >>> def f2(): ...   [i in range(5)] ...  >>> dis.dis(f1)   2           0 setup_loop              33 (to 36)               3 load_global              0 (range)               6 load_const               1 (5)               9 call_function            1              12 get_iter                     >>   13 for_iter                19 (to 35)              16 store_fast               0 (i)    3          19 load_global              1 (l)              22 load_attr                2 (append)              25 load_fast                0 (i)              28 call_function            1              31 pop_top                           32 jump_absolute           13         >>   35 pop_block                    >>   36 load_const               0 (none)              39 return_value         >>> dis.dis(f2)   2           0 build_list               0               3 load_global              0 (range)               6 load_const               1 (5)               9 call_function            1              12 get_iter                     >>   13 for_iter                12 (to 28)              16 store_fast               0 (i)              19 load_fast                0 (i)              22 list_append              2              25 jump_absolute           13         >>   28 pop_top                           29 load_const               0 (none)              32 return_value         >>>  

you can see in byte-code 22 have append attribute in first function since have not such thing in second function using list comprehension.

also note you'll have append attr loading in each iteration makes code takes approximately 2 time slower second function using list comprehension.

moreover, depending on python , code, list comprehensions might run faster manual for loop statements (often twice fast) because iterations performed @ c language speed inside interpreter, rather manual python code. larger data sets, there major performance advantage using expression.**


*you can read more efficiency of list comprehensions

** learning python mark lutz


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -