Skip to content

Instantly share code, notes, and snippets.

@domodomodomo
Created August 15, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domodomodomo/d531ed624396313d3d5a0fdb5d62b0bd to your computer and use it in GitHub Desktop.
Save domodomodomo/d531ed624396313d3d5a0fdb5d62b0bd to your computer and use it in GitHub Desktop.
import random
import time
N = 100000
lst1 = [random.randint(0, 9) for _ in range(N)]
lst2 = lst1.copy()
# insert
start1 = time.time()
lst = lst1
new_lst = []
while lst:
e = lst.pop()
if e % 2 == 0:
new_lst.insert(0, e)
end1 = time.time()
# append, reverse
start2 = time.time()
lst = lst1
new_lst = []
while lst:
e = lst.pop()
if e % 2 == 0:
new_lst.append(e)
else:
new_lst.reverse()
end2 = time.time()
#
print('insert:', end1 - start1)
print('append, reverse:', end2 - start2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment