差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
| articles:numpy_vs_geo [2021/05/21 00:02] – Takashi Suehiro | articles:numpy_vs_geo [2024/03/28 14:43] (現在) – [結論] Takashi Suehiro | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | ====== numpyとgeo.pyの速度比較 | + | ====== numpyとgeo.pyの速度比較 |
| + | ===== – numpy はリストと比べて速いのか – ===== | ||
| ===== 目的 ===== | ===== 目的 ===== | ||
| - | ベクトル,回転行列,座標変換行列といった三次元幾何演算をpythonのリストをベースに[[./geo.py|geo.py]]というモジュールを自作している.リストベースの処理よりnumpyを使ったほうが高速なのではないかとの疑念もあるので比較を行う. geo.pyはもともとpython2で開発されたが,単純な構造なのでpython3でも問題なく動く. | + | ベクトル,回転行列,座標変換行列といった三次元幾何演算をpythonのリストをベースに[[upload_files: |
| + | geo.pyはもともとpython2で開発されたが,単純な構造なのでpython3でも問題なく動く. | ||
| - | このテストは以下の条件で行った. | + | このテストは以下の条件で行った. |
| + | * ProBook 474s | ||
| + | * メモリ:8 GB | ||
| + | * CPU:Core™ i5-3230M | ||
| + | * OS: Ubuntu 20.04 | ||
| + | * jupyter notebook,python3 | ||
| このipynb自身は,[[./ | このipynb自身は,[[./ | ||
| 行 86: | 行 94: | ||
| <code python> | <code python> | ||
| data = [] | data = [] | ||
| + | </ | ||
| + | <code python> | ||
| + | def judge(test_name, | ||
| + | if g_time < np_time : | ||
| + | judgment = " | ||
| + | elif g_time > np_time : | ||
| + | judgment = " | ||
| + | else : | ||
| + | judgment = " | ||
| + | return test_name, g_time, np_time, judgment | ||
| </ | </ | ||
| ===== ループ回数の決定 ===== | ===== ループ回数の決定 ===== | ||
| 行 93: | 行 111: | ||
| </ | </ | ||
| < | < | ||
| - | 0.00014853477478027344 | + | 0.00030612945556640625 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 99: | 行 117: | ||
| </ | </ | ||
| < | < | ||
| - | 0.002180337905883789 | + | 0.0015869140625 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 105: | 行 123: | ||
| </ | </ | ||
| < | < | ||
| - | 0.017717838287353516 | + | 0.024413347244262695 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 111: | 行 129: | ||
| </ | </ | ||
| < | < | ||
| - | 0.13070940971374512 | + | 0.1378471851348877 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 117: | 行 135: | ||
| </ | </ | ||
| < | < | ||
| - | 1.1683580875396729 | + | 1.200444221496582 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 123: | 行 141: | ||
| </ | </ | ||
| < | < | ||
| - | 12.219730377197266 | + | 11.367036819458008 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 129: | 行 147: | ||
| </ | </ | ||
| < | < | ||
| - | 112.70604348182678 | + | 112.89321899414062 |
| </ | </ | ||
| 百万回ぐらいでループ前後のオーバーヘッドの影響が少なくなってきている. まだ多少影響はあるが,一千万,一億は時間がかかるし, どうせループ内の処理の影響は消せないので百万回に決定する. | 百万回ぐらいでループ前後のオーバーヘッドの影響が少なくなってきている. まだ多少影響はあるが,一千万,一億は時間がかかるし, どうせループ内の処理の影響は消せないので百万回に決定する. | ||
| 行 155: | 行 173: | ||
| </ | </ | ||
| < | < | ||
| - | 1.1506123542785645 | + | 1.143357515335083 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 162: | 行 180: | ||
| </ | </ | ||
| < | < | ||
| - | 0.6568763256072998 | + | 0.6900453567504883 |
| </ | </ | ||
| <code python> | <code python> | ||
| - | if g_time < np_time : | + | data.append(judge(' |
| - | win = " | + | |
| - | elif g_time > np_time : | + | |
| - | win = " | + | |
| - | else : | + | |
| - | win = " | + | |
| - | data.append([' | + | |
| </ | </ | ||
| ===== ベクトルの内積 geo.pyの勝ち ===== | ===== ベクトルの内積 geo.pyの勝ち ===== | ||
| 行 192: | 行 204: | ||
| </ | </ | ||
| < | < | ||
| - | 0.597369909286499 | + | 0.6457569599151611 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 199: | 行 211: | ||
| </ | </ | ||
| < | < | ||
| - | 1.6821990013122559 | + | 1.7959060668945312 |
| </ | </ | ||
| <code python> | <code python> | ||
| - | if g_time < np_time : | + | data.append(judge(' |
| - | win = " | + | |
| - | elif g_time > np_time : | + | |
| - | win = " | + | |
| - | else : | + | |
| - | win = " | + | |
| - | data.append([' | + | |
| </ | </ | ||
| ===== ベクトルの外積 geo.pyの圧勝 ===== | ===== ベクトルの外積 geo.pyの圧勝 ===== | ||
| 行 231: | 行 237: | ||
| </ | </ | ||
| < | < | ||
| - | 1.786348819732666 | + | 1.6717863082885742 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 238: | 行 244: | ||
| </ | </ | ||
| < | < | ||
| - | 52.208454847335815 | + | 54.95365524291992 |
| </ | </ | ||
| <code python> | <code python> | ||
| - | if g_time < np_time : | + | data.append(judge(' |
| - | win = " | + | |
| - | elif g_time > np_time : | + | |
| - | win = " | + | |
| - | else : | + | |
| - | win = " | + | |
| - | data.append([' | + | |
| </ | </ | ||
| ===== 行列とベクトルの積 npの勝ち ===== | ===== 行列とベクトルの積 npの勝ち ===== | ||
| 行 268: | 行 268: | ||
| </ | </ | ||
| < | < | ||
| - | 2.4420416355133057 | + | 2.362830638885498 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 275: | 行 275: | ||
| </ | </ | ||
| < | < | ||
| - | 1.813652753829956 | + | 1.848921537399292 |
| </ | </ | ||
| <code python> | <code python> | ||
| - | if g_time < np_time : | + | data.append(judge(' |
| - | win = " | + | |
| - | elif g_time > np_time : | + | |
| - | win = " | + | |
| - | else : | + | |
| - | win = " | + | |
| - | data.append([' | + | |
| </ | </ | ||
| ===== 行列同士の積 npの勝ち ===== | ===== 行列同士の積 npの勝ち ===== | ||
| 行 307: | 行 301: | ||
| </ | </ | ||
| < | < | ||
| - | 6.281814098358154 | + | 5.8199920654296875 |
| </ | </ | ||
| <code python> | <code python> | ||
| 行 314: | 行 308: | ||
| </ | </ | ||
| < | < | ||
| - | 2.4157330989837646 | + | 2.266876697540283 |
| </ | </ | ||
| <code python> | <code python> | ||
| - | if g_time < np_time : | + | data.append(judge(' |
| - | win = " | + | |
| - | elif g_time > np_time : | + | |
| - | win = " | + | |
| - | else : | + | |
| - | win = " | + | |
| - | data.append([' | + | |
| </ | </ | ||
| ===== 結論 ===== | ===== 結論 ===== | ||
| 行 333: | 行 321: | ||
| df | df | ||
| </ | </ | ||
| - | < | + | |
| <div> | <div> | ||
| <style scoped> | <style scoped> | ||
| 行 354: | 行 343: | ||
| </th> | </th> | ||
| <th> | <th> | ||
| - | </ | + | |
| 項目 | 項目 | ||
| - | < | + | |
| </th> | </th> | ||
| <th> | <th> | ||
| - | </ | + | |
| geo.py | geo.py | ||
| - | < | + | |
| </th> | </th> | ||
| <th> | <th> | ||
| - | </ | + | |
| np | np | ||
| - | < | + | |
| </th> | </th> | ||
| <th> | <th> | ||
| - | </ | + | |
| 結果 | 結果 | ||
| - | < | + | |
| </th> | </th> | ||
| </tr> | </tr> | ||
| 行 378: | 行 367: | ||
| <tr> | <tr> | ||
| <th> | <th> | ||
| - | </ | + | |
| 0 | 0 | ||
| - | < | + | |
| </th> | </th> | ||
| <td> | <td> | ||
| - | </ | + | |
| ベクトルの和 | ベクトルの和 | ||
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 1.150612 | + | 1.143358 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 0.656876 | + | 0.690045 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| npの勝ち | npの勝ち | ||
| - | < | + | |
| </td> | </td> | ||
| </tr> | </tr> | ||
| <tr> | <tr> | ||
| <th> | <th> | ||
| - | </ | + | |
| - | 1 | + | |
| - | < | + | |
| </th> | </th> | ||
| <td> | <td> | ||
| - | </ | + | |
| ベクトルの内積 | ベクトルの内積 | ||
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 0.597370 | + | 0.645757 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 1.682199 | + | 1.795906 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| geo.pyの勝ち | geo.pyの勝ち | ||
| - | < | + | |
| </td> | </td> | ||
| </tr> | </tr> | ||
| <tr> | <tr> | ||
| <th> | <th> | ||
| - | </ | + | |
| 2 | 2 | ||
| - | < | + | |
| </th> | </th> | ||
| <td> | <td> | ||
| - | </ | + | |
| ベクトルの外積 | ベクトルの外積 | ||
| < | < | ||
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 1.786349 | + | 1.671786 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 52.208455 | + | 54.953655 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| geo.pyの勝ち | geo.pyの勝ち | ||
| - | < | + | |
| </td> | </td> | ||
| </tr> | </tr> | ||
| <tr> | <tr> | ||
| <th> | <th> | ||
| - | </ | + | |
| 3 | 3 | ||
| - | < | + | |
| </th> | </th> | ||
| <td> | <td> | ||
| - | </ | + | |
| 行列とベクトルの積 | 行列とベクトルの積 | ||
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 2.442042 | + | 2.362831 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 1.813653 | + | 1.848922 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| npの勝ち | npの勝ち | ||
| - | < | + | |
| </td> | </td> | ||
| </tr> | </tr> | ||
| <tr> | <tr> | ||
| <th> | <th> | ||
| - | </ | + | |
| 4 | 4 | ||
| - | < | + | |
| </th> | </th> | ||
| <td> | <td> | ||
| - | </ | + | |
| 行列同士の積 | 行列同士の積 | ||
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 6.281814 | + | 5.819992 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| - | 2.415733 | + | 2.266877 |
| - | < | + | |
| </td> | </td> | ||
| <td> | <td> | ||
| - | </ | + | |
| npの勝ち | npの勝ち | ||
| - | < | + | |
| </td> | </td> | ||
| </tr> | </tr> | ||
| 行 515: | 行 503: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| 結論から言うと,意外にに差がないということが分かる. 大きなサイズのデータを扱うときはnumpyが良いのだろうが,三次元のベクトルや行列では大きな差は出ない. | 結論から言うと,意外にに差がないということが分かる. 大きなサイズのデータを扱うときはnumpyが良いのだろうが,三次元のベクトルや行列では大きな差は出ない. | ||
| 行 521: | 行 510: | ||
| ロボットのプログラムで使うときは個々の要素へのアクセスも多くあるので,なおさら差が出にくく現状のgeo.pyで十分であると考えられる. | ロボットのプログラムで使うときは個々の要素へのアクセスも多くあるので,なおさら差が出にくく現状のgeo.pyで十分であると考えられる. | ||
| + | <table tab_label> | ||
| + | < | ||
| + | ^ 項目 ^ geo.py ^ np ^ 結果 ^ | ||
| + | | foo | bar | | ||
| + | </ | ||