#2:deque还提供了append 和pop方法,在序列的两端都能以相同的速度执行
#例子2:使用deque实现fifo(先进先出),如下:
from collections import deque
import profile,stat
import sys
qeque=deque()
def add1(data):
qeque.append(data)
def add2():
qeque.pop()
big_n=100000
@profile(‘deque‘)
def seq():
for i in range(big_n):
add1(i)
for i in range(big_n/2):
add2()
for i in range(big_n):
add1(i)
l=[]
def add3(data):
l.append(data)
def data4():
l.pop(-1)
@profile(‘list‘)
def lse():
for i in range(big_n):
add3(i)
for i in range(big_n/2):
data4()
for i in range(big_n):
add3(i)
print seq(),lse()
print stat[‘deque‘]
print stat[‘list‘]
时间: 2025-01-11 22:14:57