Last active
August 29, 2015 14:22
-
-
Save knuu/28e9e7fb794fa4958da9 to your computer and use it in GitHub Desktop.
timevs_queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
code_list = """ | |
import random | |
l = [random.randint(1, 1000) for _ in range(1000)] | |
que = list() | |
for x in l: que.append(x) | |
for _ in range(1000): que.pop(0) | |
""" | |
code_deque = """ | |
import collections, random | |
que = collections.deque() | |
l = [random.randint(1, 1000) for _ in range(1000)] | |
for x in l: que.append(x) | |
for _ in range(1000): que.popleft() | |
""" | |
code_Queue = """ | |
import queue, random | |
que = queue.Queue() | |
l = [random.randint(1, 1000) for _ in range(1000)] | |
for x in l: que.put(x) | |
for _ in range(1000): que.get() | |
""" | |
print('{:20}{:.3f}s'.format('list:', timeit.timeit(stmt=code_list, number=1000))) | |
print('{:20}{:.3f}s'.format('collections.deque:', timeit.timeit(stmt=code_deque, number=1000))) | |
print('{:20}{:.3f}s'.format('queue.Queue:', timeit.timeit(stmt=code_Queue, number=1000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment