0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで一様乱数の配列を生成するときの速度比較

Posted at

やること

一様乱数の配列を生成するための時間を比較する

一様分布とは。

一様乱数を生成するために使用したライブラリ

  • numpy.random.uniform

  • numpy.random.default_rng (Numpy version1.17以降で推奨されているようです)

  • scipy.stats.uniform

他に、random.uniformもありますが、要素数を指定できないように見えたため、今回は除外します。

コード

numpy.random.uniform

import numpy as np
import time

array1d_size = 1_000_000 # 要素数

start_time = time.perf_counter()
np_uniform = np.random.uniform(0, 1, array1d_size)
end_time = time.perf_counter()
elapsed_time = end_time - start_time

print(f"numpy.random.uniform : {elapsed_time}[sec]")
# numpy.random.uniform : 0.008119800128042698[sec]

numpy.random.default_rng

import numpy as np
import time

rng = np.random.default_rng()

array1d_size = 1_000_000  # 要素数

start_time = time.perf_counter()
rng_uniform = rng.uniform(0, 1, array1d_size)
end_time = time.perf_counter()
elapsed_time = end_time - start_time

print(f"numpy.random.default_rng : {elapsed_time}[sec]")
# numpy.random.uniform : 0.004869400057941675[sec]

scipy.stats.uniform

from scipy.stats import uniform
import time

array1d_size = 1_000_000  # 要素数

start_time = time.perf_counter()
scipy_uniform = uniform.rvs(size=array1d_size)
end_time = time.perf_counter()
elapsed_time = end_time - start_time

print(f"scipy.stats.uniform : {elapsed_time}[sec]")
# scipy.stats.uniform : 0.012748599983751774[sec]

結果

ライブラリ 計算時間
numpy.random.uniform 0.008119800128042698[sec]
numpy.random.default_rng 0.004869400057941675[sec]
scipy.stats.uniform 0.012748599983751774[sec]

条件:配列の要素数 1,000,000
試行回数:1回(本当は何回もやるのが必要だと思いますが...)

numpy.random.default_rng を使うのが最も速い。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?