以下兩段code從執行效率上可以看出list對尾巴方向的操作的確會比較快
# slow.py
import cProfile
def xf(num):
x = range(num)
while x:
x[:10] = [] # main test snippet
cProfile.runctx("xf(100000)", globals(), locals())
# fast.py
import cProfile
def xf(num):
x = range(num)
while x:
x[-10:] = [] # main test snippet
cProfile.runctx("xf(100000)", globals(), locals())
import cProfile
def xf(num):
x = range(num)
while x:
x[-10:] = [] # main test snippet
cProfile.runctx("xf(100000)", globals(), locals())