python融合list速度比較

list comprehension 快許多,大概是因爲少了中間建立新list的步驟

橫座標數據規模,縱座標秒

 

import timeit
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm

def s():
    a = sum([li for li in l],[])
    return a

def c():
    a = [i for ii in l for i in ii]
    return a

x = 13
ds = []
dc = []
for i in tqdm(range(x)):
    l = [[0]*100 for ii in range(2**i)]
    ds.append(timeit.timeit(s, number=10,globals=globals()))
    dc.append(timeit.timeit(c, number=10,globals=globals()))
xx = [2**xi for xi in range(x)]

plt.plot(xx,[i/10 for i in ds],label='sum(lists,[])')
plt.plot(xx,[i/10 for i in dc],label='[i for list in lists for i in list]')
plt.legend()
[in] 
dis.dis(s)

[out]  
6           0 LOAD_GLOBAL              0 (sum)
              2 LOAD_CONST               1 (<code object <listcomp> at 0x000002014C0839C0, file "<ipython-input-30-1141e4ba38b0>", line 6>)
              4 LOAD_CONST               2 ('s.<locals>.<listcomp>')
              6 MAKE_FUNCTION            0
              8 LOAD_GLOBAL              1 (l)
             10 GET_ITER
             12 CALL_FUNCTION            1
             14 BUILD_LIST               0
             16 CALL_FUNCTION            2
             18 STORE_FAST               0 (a)

  7          20 LOAD_FAST                0 (a)
             22 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x000002014C0839C0, file "<ipython-input-30-1141e4ba38b0>", line 6>:
  6           0 BUILD_LIST               0
              2 LOAD_FAST                0 (.0)
        >>    4 FOR_ITER                 8 (to 14)
              6 STORE_FAST               1 (li)
              8 LOAD_FAST                1 (li)
             10 LIST_APPEND              2
             12 JUMP_ABSOLUTE            4
        >>   14 RETURN_VALUE

 

[in] dis.dis(c)
[out]
 10           0 LOAD_CONST               1 (<code object <listcomp> at 0x000002014C083540, file "<ipython-input-30-1141e4ba38b0>", line 10>)
              2 LOAD_CONST               2 ('c.<locals>.<listcomp>')
              4 MAKE_FUNCTION            0
              6 LOAD_GLOBAL              0 (l)
              8 GET_ITER
             10 CALL_FUNCTION            1
             12 STORE_FAST               0 (a)

 11          14 LOAD_FAST                0 (a)
             16 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x000002014C083540, file "<ipython-input-30-1141e4ba38b0>", line 10>:
 10           0 BUILD_LIST               0
              2 LOAD_FAST                0 (.0)
        >>    4 FOR_ITER                18 (to 24)
              6 STORE_FAST               1 (ii)
              8 LOAD_FAST                1 (ii)
             10 GET_ITER
        >>   12 FOR_ITER                 8 (to 22)
             14 STORE_FAST               2 (i)
             16 LOAD_FAST                2 (i)
             18 LIST_APPEND              3
             20 JUMP_ABSOLUTE           12
        >>   22 JUMP_ABSOLUTE            4
        >>   24 RETURN_VALUE
​

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章