階乗の限界と計算速度を Python, Julia, R, Octave で比較してみた
追記: 2022/08/18 R での結果を更新。x86-64 版でやったら早々にクラッシュしていた。M1 チップ対応版で Julia と同等以上の速度である。
# Python 3.10.6
from math import factorial
from time import time
s = time()
a = factorial(100000)
print(time() - s)
0.1885082721710205 sec.
# Julia 1.7.3
@time a = factorial(big(100000));
0.011688 seconds (302 allocations: 3.076 MiB)
# R 4.2.1 (M1)
library(gmp)
> system.time({a = factorialZ(100000)})
ユーザ システム 経過
0.009999999999999787 0.000000000000000000 0.009999999999990905
# Octave 6.2.0
tic
a = factorial(18); # 整数値解を表示するにはこれが限界
toc
Elapsed time is 0.00777102 seconds.
ということで,Julia と R の圧勝...かな?
Julia も R も,gmp を使っているので,Python も gmpy を使えば同じ速度になるのかも知れない。が,現在の環境では pip install gmpy はできるのだが,いざ import gmpy すると,エラーになるので,確認できない。