一、结论
'''
本程序模拟比较队列的三种情况:
一、deque当作队列使用;
二、queue当作队列使用;
三、list当作队列使用。
结论:
Python队列可以用deque、queue、list实现,其中list极慢、queue较慢,deque最快。
经测试结论为:deque比queue快20倍以上,deque比list快1000倍以上,queue比list快40倍以上。测试说明:云服务器2C4G,CentOs7.9为测试环境,数据量为100 0000,结论仅供参考。
'''
二、测试数据
三、测试过程
'''
本程序模拟比较队列的三种情况:
一、deque当作队列使用;
二、queue当作队列使用;
三、list当作队列使用。
结论:
Python队列可以用deque、queue、list实现,其中list极慢、queue较慢,deque最快。
经测试结论为:deque比queue快20倍以上,deque比list快1000倍以上,queue比list快40倍以上。
'''
from collections import *
from queue import *
from time import *
n = 1000000
class test():
def test_deque(self):
'''
test the time spent on executing deque.
'''
global n
start = time()
q = deque()
for i in range(n):
q.append(i)
for i in range(n):
q.popleft()
end = time()
print('The time spent on executing deque is: %f' % (end - start))
return end - start
def test_queue(self):
'''
test the time spent on executing queue.
'''
global n
start = time()
q = Queue()
for i in range(n):
q.put(i)
for i in range(n):
q.get()
end = time()
print('The time spent on executing queue is: %f' % (end - start))
return end - start
def test_list(self):
'''
test the time spent on executing list.
'''
start = time()
global n
q = []
for i in range(n):
q.append(i)
for i in range(n):
q.pop(0)
end = time()
print('The time spent on executing list is: %f' % (end - start))
return end - start
def so_the_result_of_test_is(self):
global n
deque_time = self.test_deque()
queue_time = self.test_queue()
list_time = self.test_list()
compare_deque_queue = queue_time / deque_time
compare_deque_list = list_time / deque_time
compare_queue_list = list_time / queue_time
print('When the data volume is ' + str(n) + ', queque is slower than deque:%f' % compare_deque_queue)
print('When the data volume is ' + str(n) + ', list is slower than deque: %f' % compare_deque_list)
print('When the data volume is ' + str(n) + ', list is slower than queue: %f' % compare_queue_list)
t = test()
t.so_the_result_of_test_is()