LoginSignup
0
1

More than 1 year has passed since last update.

重心座標の計算速度(pythonコード比較)(3次元空間)

Posted at

比較するコード

  • np.mean
  • np.average
  • np.sumしてから粒子数で割る(jitあり)

np.meannp.averageはnumbaにサポートされていない。
したがって jitで高速化できない。

方法

粒子数は 10,000個で、座標の値はランダム:pos = np.random.rand(10000,3)
3つめのnp.sumの関数は次の通り。

@njit
def calc_CoM(pos):
    return np.sum(pos, axis=0) / len(pos)

%%timeitを使用し、10,000回の繰り返しを100回実行した平均値を結果として次章に著す。

結果

コード 平均計算時間 1位との比 ランキング
np.mean 125 μs 5.2 3位
np.average 122 μs 5.0 2位
np.sum / len+jit 24.2 μs 1 1位

numpyのnp.meannp.averageを使うよりも、numbaが対応している関数だけを使ってjitで高速化したほうが速いことがわかった。

上のランキング表には記載しなかったが、不思議なことに、
np.averageの計算速度の標準偏差は 1.45 μs なのに対し、
np.mean15.6 μs と、10倍程度 長かった。

私の手法が統計的に十分かどうかはわからないが、この結果は
np.averageの方が安定した計算速度を出せるということを示す。

参考

np.meannp.averageの違いは、例えばこのリンクを見るとわかる。

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