Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Created May 18, 2020 13:36
Show Gist options
  • Save paulwinex/8d3d9a4cc549b55c48fc19f7e59734ba to your computer and use it in GitHub Desktop.
Save paulwinex/8d3d9a4cc549b55c48fc19f7e59734ba to your computer and use it in GitHub Desktop.
from collections import deque
import time
# append right
print('Test append right')
st = time.perf_counter()
for _ in range(1000):
l1 = deque()
for i in range(5000):
l1.append(i)
en = round(time.perf_counter()-st, 3)
print(f'Test list: {en}sec')
st = time.perf_counter()
for _ in range(1000):
l2 = list()
for i in range(5000):
l2.append(i)
en = round(time.perf_counter()-st, 3)
print(f'Test deque: {en}sec')
# append left
print('Test append left')
st = time.perf_counter()
for _ in range(1000):
l1 = deque()
for i in range(5000):
l1.appendleft(i)
en = round(time.perf_counter()-st, 3)
print(f'Test list: {en}sec')
st = time.perf_counter()
for _ in range(1000):
l2 = list()
for i in range(5000):
l2.insert(0, i)
en = round(time.perf_counter()-st, 3)
print(f'Test deque: {en}sec')
# pop right
print('Test pop right')
st = time.perf_counter()
for _ in range(1000):
l1 = deque()
l1.extend(list(range(5000)))
while l1:
l1.pop()
en = round(time.perf_counter()-st, 3)
print(f'Test deque: {en}sec')
st = time.perf_counter()
for _ in range(1000):
l2 = list()
l2.extend(list(range(5000)))
while l2:
l2.pop()
en = round(time.perf_counter()-st, 3)
print(f'Test list: {en}sec')
# pop left
print('Test pop left')
st = time.perf_counter()
for _ in range(1000):
l1 = deque()
l1.extend(list(range(5000)))
while l1:
l1.popleft()
en = round(time.perf_counter()-st, 3)
print(f'Test deque: {en}sec')
st = time.perf_counter()
for _ in range(1000):
l2 = list()
l2.extend(list(range(5000)))
while l2:
l2.pop(0)
en = round(time.perf_counter()-st, 3)
print(f'Test list: {en}sec')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment