LoginSignup
0
0

More than 5 years have passed since last update.

pybind11で二項分布を計算してみる。

Posted at

このテキストについて

scipy 1.1.0 の binom.pmfをc部分を抜き出し、pybind11で直接bindして速度計測したもの。
内容はGithub repositoryにある。

scipyのc部分

おおよそここにあるガンマ関数のlogを写したものを使う。定数などは適宜変更してある。結果はここ
に示した通り。

計算実験

python上(jupyter上)で行う。

from binomial import binomial
from scipy.stats import binom
# 計算用のparameter
params = (4, 30, 0.3)
binomial(*params) == binom.pmf(*params) # ==> True

%%timeit
binomial(*params)
# ==> 420 ns ± 10.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
binom.pmf(*params)
# ==> 97.3 µs ± 1.78 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

100倍ぐらいの速度差が出ている。

終わりに

とりあえずやって見たけれど、想像した以上の差が出た。
なぜこの差が出るかは後ほどプロファイルしてみる予定。

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