LoginSignup
3
1

More than 5 years have passed since last update.

NumPyのソートの速度比較

Posted at

概要

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]

これだけ。

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1