Created
January 21, 2016 12:21
-
-
Save sasanquaneuf/5d1df3e781a3b11c01dd to your computer and use it in GitHub Desktop.
Python(2.7)で更新可能な優先度つきキューを作りたい ref: http://qiita.com/sasanquaneuf/items/4850a1fd9357d224c5df
This file contains hidden or 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
# Nの乱数列を生成 | |
import random | |
N = 100000 | |
M = 100000 | |
target = range(0,N) | |
temp = range(0,N) | |
for n in range(N-1,-1,-1): | |
target[n] = temp.pop(random.randint(0,n)) |
This file contains hidden or 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
# Nの乱数列を生成 | |
import random | |
N = 1000000 | |
M = 40000 | |
target = range(0,N) | |
for n in range(N-1,-1,-1): | |
target[n] = random.randint(0,M-1) |
This file contains hidden or 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
# Nの乱数列を生成 | |
import random | |
N = 1000000 | |
M = 4000 | |
target = range(0,N) | |
for n in range(N-1,-1,-1): | |
target[n] = random.randint(0,M-1) |
This file contains hidden or 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 Queue | |
import heapq | |
import datetime | |
time1 = d.now() | |
for n in range(0,M): | |
t.get() | |
time2 = d.now() | |
for n in range(0,M): | |
heapq.heappop(u) | |
time3 = d.now() | |
for n in range(0,M): | |
tepop(v, dd) | |
time4 = d.now() | |
print('pop:Queue',(time2 - time1).__str__()) | |
print('pop:Heap',(time3 - time2).__str__()) | |
print('pop:TeQ',(time4 - time3).__str__()) |
This file contains hidden or 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
('pop:Queue', '0:00:00.484000') | |
('pop:Heap', '0:00:00.214000') | |
('pop:TeQ', '0:00:00.299000') |
This file contains hidden or 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 Queue | |
import heapq | |
import datetime | |
d = datetime.datetime(1,1,1) | |
time1 = d.now() | |
t = Queue.PriorityQueue(N) | |
for n in range(0,N): | |
t.put((target[n], n)) | |
time2 = d.now() | |
u = [] | |
for n in range(0,N): | |
heapq.heappush(u, (target[n], n)) | |
time3 = d.now() | |
v = [] | |
dd = {} | |
for n in range(0,N): | |
v = tepush(v, dd, 2*M, target[n], n) | |
time4 = d.now() | |
print('push:Queue',(time2 - time1).__str__()) | |
print('push:Heap',(time3 - time2).__str__()) | |
print('push:TeQ',(time4 - time3).__str__()) |
This file contains hidden or 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
('push:Queue', '0:00:00.350000') | |
('push:Heap', '0:00:00.086000') | |
('push:TeQ', '0:00:00.137000') |
This file contains hidden or 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 heapq | |
def tepush(l, d, m, sortkey, value): | |
if not value in d: | |
generation = 1 | |
else: | |
generation = d[value] + 1 | |
d[value] = generation | |
heapq.heappush(l, (sortkey, value, generation)) | |
length = len(l) | |
if length > m: # ヒープの再構成 | |
lt = [] | |
for n in range(0,length): | |
if l[n][2] == d[l[n][1]]: | |
heapq.heappush(lt, (l[n][0], l[n][1], 1)) | |
d[l[n][1]] = 1 | |
return lt | |
else: | |
return l | |
def tepop(l, d): | |
while len(l) > 0: | |
(sortkey, value, generation) = heapq.heappop(l) | |
if d[value] == generation: | |
return (sortkey, value, generation) |
This file contains hidden or 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
d = datetime.datetime(1,1,1) | |
for m in [2,5,10,20]: | |
time1 = d.now() | |
v = [] | |
dd = {} | |
for n in range(0,N): | |
v = tepush(v, dd, m*M, n, target[n]) | |
time2 = d.now() | |
for n in range(0,M): | |
tepop(v, dd) | |
time3 = d.now() | |
print('push:TeQ_' + str(m),(time2 - time1).__str__()) | |
print('pop:TeQ_' + str(m),(time3 - time2).__str__()) |
This file contains hidden or 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
d = datetime.datetime(1,1,1) | |
for m in [2,5,10,20]: | |
time1 = d.now() | |
v = [] | |
dd = {} | |
for n in range(0,N): | |
v = tepush(v, dd, m*M, n, target[n]) | |
time2 = d.now() | |
for n in range(0,M): | |
tepop(v, dd) | |
time3 = d.now() | |
print('push:TeQ_' + str(m),(time2 - time1).__str__()) | |
print('pop:TeQ_' + str(m),(time3 - time2).__str__()) |
This file contains hidden or 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
('push:TeQ_2', '0:00:02.731000') | |
('pop:TeQ_2', '0:00:00.193000') | |
('push:TeQ_5', '0:00:01.794000') | |
('pop:TeQ_5', '0:00:00.527000') | |
('push:TeQ_10', '0:00:01.667000') | |
('pop:TeQ_10', '0:00:00.711000') | |
('push:TeQ_20', '0:00:01.605000') | |
('pop:TeQ_20', '0:00:00.581000') |
This file contains hidden or 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
('push:TeQ_2', '0:00:02.927000') | |
('pop:TeQ_2', '0:00:00.013000') | |
('push:TeQ_5', '0:00:02.035000') | |
('pop:TeQ_5', '0:00:00.018000') | |
('push:TeQ_10', '0:00:01.929000') | |
('pop:TeQ_10', '0:00:00.093000') | |
('push:TeQ_20', '0:00:01.846000') | |
('pop:TeQ_20', '0:00:00.026000') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment