Skip to content

Instantly share code, notes, and snippets.

@Shemeikka
Created November 22, 2017 16:36
Show Gist options
  • Save Shemeikka/30371503a385fbf5ed5678bb00f78d70 to your computer and use it in GitHub Desktop.
Save Shemeikka/30371503a385fbf5ed5678bb00f78d70 to your computer and use it in GitHub Desktop.
Small benchmarks for Python queues
from collections import deque
import Queue
import time
import gevent.queue
def queue_test(n):
q = Queue.Queue()
start = time.time()
for i in range(0, n):
q.put_nowait(i)
for i in range(0, n):
q.get_nowait()
print "Queue: {} loops took {}".format(n, time.time() - start)
def priority_queue_test(n):
q = Queue.PriorityQueue()
start = time.time()
for i in range(0, n):
q.put_nowait((100, time.time(), i))
for i in range(0, n):
q.get_nowait()
print "Priority queue: {} loops took {}".format(n, time.time() - start)
def deque_test(n):
q = deque()
start = time.time()
for i in range(0, n):
q.append(i)
for i in range(0, n):
q.popleft()
print "Deque: {} loops took {}".format(n, time.time() - start)
def gevent_queue_test(n):
q = gevent.queue.Queue()
start = time.time()
for i in range(0, n):
q.put_nowait(i)
for i in range(0, n):
q.get_nowait()
print "Gevent queue: {} loops took {}".format(n, time.time() - start)
def gevent_priority_queue_test(n):
q = gevent.queue.PriorityQueue()
start = time.time()
for i in range(0, n):
q.put_nowait((100, time.time(), i))
for i in range(0, n):
q.get_nowait()
print "Gevent priority queue: {} loops took {}".format(n, time.time() - start)
def main():
queue_test(10000000)
priority_queue_test(10000000)
deque_test(10000000)
gevent_queue_test(10000000)
gevent_priority_queue_test(10000000)
if __name__ == '__main__':
main()
#############################
"""
➜ python main.py # With 1000000 items
Queue: 1000000 loops took 1.82382798195
Priority queue: 1000000 loops took 3.33943295479
Deque: 1000000 loops took 0.101699113846
Gevent queue: 1000000 loops took 0.729159116745
Gevent priority queue: 1000000 loops took 2.1183359623
➜ python main.py # With 10000000 items
Queue: 10000000 loops took 17.9672968388
Priority queue: 10000000 loops took 35.4362130165
Deque: 10000000 loops took 1.02454304695
Gevent queue: 10000000 loops took 7.27037000656
Gevent priority queue: 10000000 loops took 24.3694839478
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment