LoginSignup
0
2

More than 3 years have passed since last update.

pythonで3億桁までの多数桁乗算時間

Last updated at Posted at 2020-02-07

pythonとC(gnuのgmp)の多数桁乗算の比較を行った。パソコン(4Ghz)での計算時間を下記に示す(単位:秒)。10進3億桁(結果)1回から桁数半分、回数倍で順に計算。共に右端の値は10進236万桁を128回計算した時間。
python  : 1627, 1084, 723, 482, 322, 215, 143, 96 (s)
gnu(gmp)  : 4.5, 3.9, 3.7, 3,4, 3.2, 2.8, 2.6, 2.2 (s)
千桁以上でpythonはカラツバ法、gmpはFMT(整数FFT)を使用しているのが分かる。pythonは桁数2倍で、3倍の時間(例は逆順1.5倍)が必要。gmpはFMT計算なので、桁数x回数でlog(桁数)比となる。
pythonのソースと両者の詳細結果は https://ecc-256.com のpythonプログラムの多数桁乗算を参照。面白いことに、10進20桁から200桁(共に回数は膨大)ではpythonは2倍程度遅いだけ。これは、共に結果を格納する場所の確保とポインターに多くの時間が必要のためだろう。
pythonはd10=format(a)でaを10進数に変換する処理が遅い。乗算が桁数2倍で3倍の時間に対し、変換は4倍かかる。118万桁、236万桁、472万桁の乗算と変換の時間を順に示す。
乗算: 0.24, 0.72, 2,19 (s), 変換: 19, 77, 308 (s)

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