articles:numpy_vs_geo

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
articles:numpy_vs_geo [2021/05/20 23:52] – 作成 Takashi Suehiroarticles:numpy_vs_geo [2024/03/28 14:43] (現在) – [結論] Takashi Suehiro
行 1: 行 1:
-====== numpyとgeo.pyの速度比較  – numpy はリストと比べて速いのか – ======+====== numpyとgeo.pyの速度比較 ====== 
 +===== – numpy はリストと比べて速いのか –  ===== 
  
 ===== 目的 ===== ===== 目的 =====
  
-ベクトル,回転行列,座標変換行列といった三次元幾何演算をpythonのリストをベースに[[./geo.py|geo.py]]というモジュールを自作している.リストベースの処理よりnumpyを使ったほうが高速なのではないかとの疑念もあるので比較を行う. geo.pyはもともとpython2で開発されたが,単純な構造なのでpython3でも問題なく動く.+ベクトル,回転行列,座標変換行列といった三次元幾何演算をpythonのリストをベースに[[upload_files:geo.py|geo.py]]というモジュールを自作している.リストベースの処理よりnumpyを使ったほうが高速なのではないかとの疑念もあるので比較を行う. \\ 
 +geo.pyはもともとpython2で開発されたが,単純な構造なのでpython3でも問題なく動く.
  
-このテストは以下の条件で行った. ProBook 474sメモリ:8 GBCPU:Core™ i5-3230MOS: Ubuntu 20.04 jupyter notebook,python3+このテストは以下の条件で行った. 
 +  * ProBook 474s 
 +  * メモリ:8 GB 
 +  * CPU:Core™ i5-3230M 
 +  * OS: Ubuntu 20.04 
 +  * jupyter notebook,python3
  
 このipynb自身は,[[./numpy_vs_geo.ipynb|numpy_vs_geo.ipynb]]となっている. このipynb自身は,[[./numpy_vs_geo.ipynb|numpy_vs_geo.ipynb]]となっている.
行 86: 行 94:
 <code python> <code python>
 data = [] data = []
 +</code>
 +<code python>
 +def judge(test_name, g_time, np_time) :
 +    if g_time < np_time :
 +        judgment = "geo.pyの勝ち"
 +    elif g_time > np_time :
 +        judgment = "npの勝ち"
 +    else :
 +        judgment = "引き分け"
 +    return test_name, g_time, np_time, judgment
 </code> </code>
 ===== ループ回数の決定 ===== ===== ループ回数の決定 =====
行 93: 行 111:
 </code> </code>
 <code> <code>
-0.0002894401550292969+0.00030612945556640625
 </code> </code>
 <code python> <code python>
行 99: 行 117:
 </code> </code>
 <code> <code>
-0.0015511512756347656+0.0015869140625
 </code> </code>
 <code python> <code python>
行 105: 行 123:
 </code> </code>
 <code> <code>
-0.01816701889038086+0.024413347244262695
 </code> </code>
 <code python> <code python>
行 111: 行 129:
 </code> </code>
 <code> <code>
-0.15154337882995605+0.1378471851348877
 </code> </code>
 <code python> <code python>
行 117: 行 135:
 </code> </code>
 <code> <code>
-1.1699960231781006+1.200444221496582
 </code> </code>
 <code python> <code python>
行 123: 行 141:
 </code> </code>
 <code> <code>
-11.127409934997559+11.367036819458008
 </code> </code>
 <code python> <code python>
行 129: 行 147:
 </code> </code>
 <code> <code>
-113.64605188369751+112.89321899414062
 </code> </code>
 百万回ぐらいでループ前後のオーバーヘッドの影響が少なくなってきている. まだ多少影響はあるが,一千万,一億は時間がかかるし, どうせループ内の処理の影響は消せないので百万回に決定する. 百万回ぐらいでループ前後のオーバーヘッドの影響が少なくなってきている. まだ多少影響はあるが,一千万,一億は時間がかかるし, どうせループ内の処理の影響は消せないので百万回に決定する.
行 155: 行 173:
 </code> </code>
 <code> <code>
-1.1759397983551025+1.143357515335083
 </code> </code>
 <code python> <code python>
行 162: 行 180:
 </code> </code>
 <code> <code>
-0.7172393798828125+0.6900453567504883
 </code> </code>
 <code python> <code python>
-if g_time < np_time : +data.append(judge('ベクトルの和', g_time, np_time))
-    win = "geo.pyの勝ち" +
-elif g_time > np_time : +
-    win = "npの勝ち" +
-else : +
-    win = "引き分け" +
-data.append(['ベクトルの和', g_time, np_time, win])+
 </code> </code>
 ===== ベクトルの内積 geo.pyの勝ち ===== ===== ベクトルの内積 geo.pyの勝ち =====
行 192: 行 204:
 </code> </code>
 <code> <code>
-0.565962553024292+0.6457569599151611
 </code> </code>
 <code python> <code python>
行 199: 行 211:
 </code> </code>
 <code> <code>
-2.0540502071380615+1.7959060668945312
 </code> </code>
 <code python> <code python>
-if g_time < np_time : +data.append(judge('ベクトルの内積', g_time, np_time))
-    win = "geo.pyの勝ち" +
-elif g_time > np_time : +
-    win = "npの勝ち" +
-else : +
-    win = "引き分け" +
-data.append(['ベクトルの内積', g_time, np_time, win])+
 </code> </code>
 ===== ベクトルの外積 geo.pyの圧勝 ===== ===== ベクトルの外積 geo.pyの圧勝 =====
行 231: 行 237:
 </code> </code>
 <code> <code>
-1.6347482204437256+1.6717863082885742
 </code> </code>
 <code python> <code python>
行 238: 行 244:
 </code> </code>
 <code> <code>
-53.34123134613037+54.95365524291992
 </code> </code>
 <code python> <code python>
-if g_time < np_time : +data.append(judge('ベクトルの外積', g_time, np_time))
-    win = "geo.pyの勝ち" +
-elif g_time > np_time : +
-    win = "npの勝ち" +
-else : +
-    win = "引き分け" +
-data.append(['ベクトルの外積', g_time, np_time, win])+
 </code> </code>
 ===== 行列とベクトルの積 npの勝ち ===== ===== 行列とベクトルの積 npの勝ち =====
行 268: 行 268:
 </code> </code>
 <code> <code>
-2.3782222270965576+2.362830638885498
 </code> </code>
 <code python> <code python>
行 275: 行 275:
 </code> </code>
 <code> <code>
-2.034985065460205+1.848921537399292
 </code> </code>
 <code python> <code python>
-if g_time < np_time : +data.append(judge('行列とベクトルの積', g_time, np_time))
-    win = "geo.pyの勝ち" +
-elif g_time > np_time : +
-    win = "npの勝ち" +
-else : +
-    win = "引き分け" +
-data.append(['行列とベクトルの積', g_time, np_time, win])+
 </code> </code>
 ===== 行列同士の積 npの勝ち ===== ===== 行列同士の積 npの勝ち =====
行 307: 行 301:
 </code> </code>
 <code> <code>
-5.95955228805542+5.8199920654296875
 </code> </code>
 <code python> <code python>
行 314: 行 308:
 </code> </code>
 <code> <code>
-2.6894664764404297+2.266876697540283
 </code> </code>
 <code python> <code python>
-if g_time < np_time : +data.append(judge('行列同士の積', g_time, np_time))
-    win = "geo.pyの勝ち" +
-elif g_time > np_time : +
-    win = "npの勝ち" +
-else : +
-    win = "引き分け" +
-data.append(['行列同士の積', g_time, np_time, win])+
 </code> </code>
 ===== 結論 ===== ===== 結論 =====
行 331: 行 319:
 <code python> <code python>
 df = pd.DataFrame(data, columns=["項目", "geo.py", "np", "結果"]) df = pd.DataFrame(data, columns=["項目", "geo.py", "np", "結果"])
-df +df
-</code> +
-<code python> +
-print(df.to_markdown()) +
-</code> +
-<code> +
-|    | 項目                 geo.py |        np | 結果         | +
-|---:|:-------------------|---------:|----------:|:-------------| +
-|  0 | ベクトルの和       | 1.17594  |  0.717239 | npの勝ち     | +
-|  1 | ベクトルの内積     | 0.565963 |  2.05405  | geo.pyの勝ち | +
-|  2 | ベクトルの外積     | 1.63475  | 53.3412   | geo.pyの勝ち | +
-|  3 | 行列とベクトルの積 | 2.37822  |  2.03499  | npの勝ち     | +
-|  4 | 行列同士の積       | 5.95955  |  2.68947  | npの勝ち     |+
 </code> </code>
 +
 +
 +<div>
 +<style scoped>
 +    .dataframe tbody tr th:only-of-type {
 +        vertical-align: middle;
 +    }
 +
 +    .dataframe tbody tr th {
 +        vertical-align: top;
 +    }
 +
 +    .dataframe thead th {
 +        text-align: right;
 +    }
 +</style>
 +<table border="1" class="dataframe">
 +<thead>
 +<tr style="text-align: right;">
 +<th>
 +</th>
 +<th>
 +
 +項目
 +
 +</th>
 +<th>
 +
 +geo.py
 +
 +</th>
 +<th>
 +
 +np
 +
 +</th>
 +<th>
 +
 +結果
 +
 +</th>
 +</tr>
 +</thead>
 +<tbody>
 +<tr>
 +<th>
 +
 +0
 +
 +</th>
 +<td>
 +
 +ベクトルの和
 +
 +</td>
 +<td>
 +
 +1.143358
 +
 +</td>
 +<td>
 +
 +0.690045
 +
 +</td>
 +<td>
 +
 +npの勝ち
 +
 +</td>
 +</tr>
 +<tr>
 +<th>
 +
 +
 +</th>
 +<td>
 +
 +ベクトルの内積
 +
 +</td>
 +<td>
 +
 +0.645757
 +
 +</td>
 +<td>
 +
 +1.795906
 +
 +</td>
 +<td>
 +
 +geo.pyの勝ち
 +
 +</td>
 +</tr>
 +<tr>
 +<th>
 +
 +2
 +
 +</th>
 +<td>
 +
 +ベクトルの外積
 +<HTML>
 +</td>
 +<td>
 +
 +1.671786
 +
 +</td>
 +<td>
 +
 +54.953655
 +
 +</td>
 +<td>
 +
 +geo.pyの勝ち
 +
 +</td>
 +</tr>
 +<tr>
 +<th>
 +
 +3
 +
 +</th>
 +<td>
 +
 +行列とベクトルの積
 +
 +</td>
 +<td>
 +
 +2.362831
 +
 +</td>
 +<td>
 +
 +1.848922
 +
 +</td>
 +<td>
 +
 +npの勝ち
 +
 +</td>
 +</tr>
 +<tr>
 +<th>
 +
 +4
 +
 +</th>
 +<td>
 +
 +行列同士の積
 +
 +</td>
 +<td>
 +
 +5.819992
 +
 +</td>
 +<td>
 +
 +2.266877
 +
 +</td>
 +<td>
 +
 +npの勝ち
 +
 +</td>
 +</tr>
 +</tbody>
 +</table>
 +</div>
 +</HTML>
 +
 結論から言うと,意外にに差がないということが分かる. 大きなサイズのデータを扱うときはnumpyが良いのだろうが,三次元のベクトルや行列では大きな差は出ない. 結論から言うと,意外にに差がないということが分かる. 大きなサイズのデータを扱うときはnumpyが良いのだろうが,三次元のベクトルや行列では大きな差は出ない.
  
行 351: 行 510:
 ロボットのプログラムで使うときは個々の要素へのアクセスも多くあるので,なおさら差が出にくく現状のgeo.pyで十分であると考えられる. ロボットのプログラムで使うときは個々の要素へのアクセスも多くあるので,なおさら差が出にくく現状のgeo.pyで十分であると考えられる.
  
 +<table tab_label>
 +<caption>まとめの表</caption>
 +^ 項目 ^ geo.py ^ np ^ 結果 ^
 +| foo    | bar    |
 +</table>
  
  • articles/numpy_vs_geo.1621522323.txt.gz
  • 最終更新: 2021/05/20 23:52
  • by Takashi Suehiro