Created
May 30, 2015 06:51
-
-
Save knuu/eafac8771b193cb775c8 to your computer and use it in GitHub Desktop.
timevs_priority_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_heapq = """ | |
import heapq, random | |
l = [random.randint(1, 1000) for _ in range(1000)] | |
pque = [] | |
for x in l: heapq.heappush(pque, x) | |
for _ in range(1000): heapq.heappop(pque) | |
""" | |
code_PriorityQueue = """ | |
import queue, random | |
l = [random.randint(1, 1000) for _ in range(1000)] | |
pque = queue.PriorityQueue() | |
for x in l: pque.put(x) | |
for _ in range(1000): pque.get() | |
""" | |
print('{:20}{:.3f}s'.format('heapq:', timeit.timeit(stmt=code_heapq, number=1000))) | |
print('{:20}{:.3f}s'.format('queue.PriorityQueue:', timeit.timeit(stmt=code_PriorityQueue, number=1000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment