Python - append VS extend efficiency

前端 未结 3 691
余生分开走
余生分开走 2020-12-28 09:13

Here is some code that I wrote using Python:

from math import sqrt
abundant_list = []

for i in range(12,28123+1):
    dividor_list = [1]
    for j in range(         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 09:24

    It is also worth pointing out that the answer to this question hinges on the small size of the list/tuple that is added on each iteration. For larger lists, extend is clearly superior (and lists vs tuples does not make a difference). Starting with mgilson's answer, I checked behaviour for collections with 600 items, rather than 2: Calling append 600 times takes 8 times as long as using extend() with a manually defined list/tuple (i.e. [v,v,v,v,v,v,v...]):

    42.4969689846
    5.45146393776
    5.38034892082
    

    The bulk of these five seconds is actually the list/tuple creation. Preparing it before the timeit call brings times for extend down to

    1.42491698265
    0.657584905624
    

    for list and tuple, respectively.

    For a more realistic (and fairer) case, one can dynamically generate the data within the function call:

    import timeit
    
    def append_loop(foo, reps):
        for i in range(reps):
            foo.append(i)
    
    def append_comp(foo, reps):
        [foo.append(i) for i in range(reps)]
    
    def extend_lst(foo, reps):
        foo.extend([i for i in range(reps)])
    
    def extend_tup(foo, reps):
        foo.extend((i for i in range(reps)))
    
    repetitions = 600
    
    print timeit.timeit('append_loop([], repetitions)', setup='from __main__ import append_loop, repetitions')
    print timeit.timeit('append_comp([], repetitions)', setup='from __main__ import append_comp, repetitions')
    print timeit.timeit('extend_lst([], repetitions)', setup='from __main__ import extend_lst, repetitions')
    print timeit.timeit('extend_tup([], repetitions)', setup='from __main__ import extend_tup, repetitions')
    

    (Append is implemented both via for-loop and list comprehension to factor out efficiency differences between the two ways of looping.)

    Timings are:

    53.8211231232
    57.1711571217
    19.8829259872
    28.5986201763
    

    As we can see, extend with list comprehension is still over two times faster than appending. Also, tuple comprehension appears noticeably slower than list comprehension, and append_comp only introduces unnecessary list creation overhead.

提交回复
热议问题