Skip to content

Instantly share code, notes, and snippets.

@WhiteRobe
Created April 29, 2020 15:52
Show Gist options
  • Save WhiteRobe/9ce5f1e6edd2b46b8f8066429aee3dc6 to your computer and use it in GitHub Desktop.
Save WhiteRobe/9ce5f1e6edd2b46b8f8066429aee3dc6 to your computer and use it in GitHub Desktop.
dequeue和list的效率比对
import time, collections
start = time.perf_counter()
a = []
for i in range(10000000):
a.insert(0, 0);a.pop(0)
print(time.perf_counter() - start)
start = time.perf_counter()
a = collections.deque([])
for i in range(10000000):
a.insert(0, 0);a.pop()
print(time.perf_counter() - start)
start = time.perf_counter()
a = []
for i in range(10000000):
a += [0]
print(time.perf_counter() - start)
start = time.perf_counter()
a = []
for i in range(10000000):
a.append(0)
print(time.perf_counter() - start)
start = time.perf_counter()
a = collections.deque([])
for i in range(10000000):
a.append(0)
print(time.perf_counter() - start)
"""
Output:
5.4079986
2.2909950000000006
1.7664375999999988
1.3421816
1.332309200000001
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment