概要
NumPyにあるソートアルゴリズムの速度比較をしてみたかった。
実装
import time
import numpy as np
from tqdm import tqdm
ITER_NUM = 1000
SEQ_LEN = 10000
for sort_name in ["quicksort", "mergesort", "heapsort"]:
time_sum = 0
for _ in tqdm(range(ITER_NUM)):
a = np.random.random_sample((SEQ_LEN,))
starttime = time.time()
np.sort(a, kind=sort_name)
endtime = time.time()
time_sum += endtime - starttime
print("%s: %f[s]" % (sort_name, time_sum))
実行するとこんな感じになる。
tqdmを使うとプログレスバーが出て楽しい。
λ python sort_comp.py
100%|███████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1427.31it/s]
quicksort: 0.610566[s]
100%|███████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1213.31it/s]
mergesort: 0.711771[s]
100%|███████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1055.30it/s]
heapsort: 0.851218[s]
これだけ。