{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " 行列の計算速度競争\n", "
\n", "
\n", " cc by Shigeto R. Nishitani, 2017-10-30\n", "\n", "
\n", "\n", "* file: ~/python/jupyter_num_calc/notebooks_python/LA_speed_race.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Mapleと，C, pythonで競争させてみました．Cはそのままです．Mapleは\n", "``` octave\n", "with(LinearAlgebra):\n", "data:=[1000,2000,4000];\n", "for n in data do\n", " A:=RandomMatrix(n,n,generator = 0..1.0):\n", " b:=RandomVector(n,generator = 0..1.0):\n", " st:=time();\n", " LUDecomposition():\n", " print(time()-st);\n", "end:\n", "```\n", "\n", "です．pythonのcodeは，lapackにあるdgesvを指定して\n", "呼び出すようにしています．scipy.laのsolveでは何を使っているかはよくわからないので．\n", "\n", "``` python\n", "import numpy as np\n", "import scipy.linalg as la\n", "import time\n", "\n", "sizes = [1000, 2000, 4000]\n", "for n in sizes:\n", " A = np.random.random((n, n))\n", " b = np.random.random((n))\n", " start = time.time()\n", " la.lapack.dgesv(A, b)\n", " print('%s [dim], %10.5f [sec] # python' % (n,time.time()-start))\n", "```\n", "\n", "結果は，次の通りです．\n", "\n", "|size|Maple[sec]|C[sec]|python[sec]|\n", "|:----|-----:|-----:|------:|\n", "|1000 | 0.339|0.0589|0.04101\n", "|2000 | 1.384|0.3826|0.12682\n", "|4000 |45.482|3.0111|0.81373\n", "\n", "pythonの圧勝ですね．絶対値はあまり意味がありません．MacBook Air(13-inch, Early 2015/2.2GHz Core i7, ElCapitan 10.11.6)ですが，softのversionにもよります．\n", "\n", "ただし，どんなrandomを生成しているかで結果は大きく変わるので，ちょっと怪しいです．\n", "たとえば，MapleのRandomMatrixでgeneratorを指定しないと\n", "size=1000でも9.773[sec]ととんでもないぐらい時間がかかります．\n", "\n", "MapleではさらにRandomMatrixの生成にも時間がかかります．\n", "MapleではNAGのライブラリを使っているので．．．\n", "\n", "ちなみにrubyでは[SciRuby](https://github.com/SciRuby/nmatrix/wiki/Getting-started)\n", "に従ってnmatrixをinstallして，\n", "```ruby\n", "require 'nmatrix'\n", "require 'time'\n", "\n", "#n = NMatrix.new(3, [4,9,2,3,5,7,8,1,6], dtype: :float64)\n", "[1000,2000,4000].each do |size|\n", " n = size\n", " aa = NMatrix.random([n,n])\n", " start = Time.now\n", " lu = aa.factorize_lu\n", " p Time.now - start\n", "end\n", "```\n", "\n", "素直に走らせると，\n", "\n", "|size|nmatrix[sec]|nmaxtrix/lapacke|\n", "|:----|-----:|-----:|\n", "|1000 | 0.2118|0.028607|\n", "|2000 | 1.9287|0.191315|\n", "|4000 |16.9059|1.313174|\n", "\n", "のnmatrixです．2017/10/28現在nmatrix-0.2.4です．\n", "やっぱ，Rubyは数値計算苦手なんかと諦めかけたんですが，\n", "\n", "```\n", "gem install nmatrix nmatrix-lapacke\n", "```\n", "\n", "して\n", "```\n", "Require 'nmatrix/lapacke'\n", "```\n", "\n", "すると前表のnmatrix/lapacke欄です．これだと，C版に遜色ないですね．\n", "nmatrix-lapacke-0.2.3がinstallされています．このあたりの解説が[sciruby](http://sciruby.com/nmatrix/)以外見当たらなくて．．．\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "30px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }