-
-
Save kkweon/01c59b140ad624e47daa5de0796e5bb3 to your computer and use it in GitHub Desktop.
jupyter_magic.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "%load_ext cython", | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%cython\n\ncpdef int fact1(int n):\n \"\"\"Doc\n \n Parameters\n ----------\n n : int\n \n \"\"\"\n if (n <= 1):\n return 1\n \n else:\n return n * fact1(n - 1)\n \ndef fact2(n):\n if (n <= 1):\n return 1\n else:\n return n * fact2(n - 1)\n \ndef fact3(int n):\n if (n <= 1):\n return 1\n else:\n return n * fact3(n - 1)\n \ncpdef int fact4(int n):\n if n <= 1:\n return 1\n else:\n return n * fact4(n - 1)", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 fact1(5)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "51.1 ns ± 1.57 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 fact2(5)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "338 ns ± 50.8 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 fact3(5)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "235 ns ± 3.75 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 fact4(5)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "57.1 ns ± 0.471 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "def fact_python(n):\n if (n <= 1):\n return 1\n else:\n return n * fact_python(n - 1)", | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 fact_python(5)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "895 ns ± 71.2 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "%quickref", | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%writefile hello.py\nprint(\"Hello World\")", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Overwriting hello.py\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# %load hello.py\nprint(\"Hello World\")", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Hello World\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%save save_test.py 2", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "File `save_test.py` exists. Overwrite (y/[N])? y\nThe following commands were written to file `save_test.py`:\nget_ipython().run_cell_magic('cython', '', '\\ncpdef int fact1(int n):\\n \"\"\"Doc\\n \\n Parameters\\n ----------\\n n : int\\n \\n \"\"\"\\n if (n <= 1):\\n return 1\\n \\n else:\\n return n * fact1(n - 1)\\n \\ndef fact2(n):\\n if (n <= 1):\\n return 1\\n else:\\n return n * fact2(n - 1)\\n \\ndef fact3(int n):\\n if (n <= 1):\\n return 1\\n else:\\n return n * fact3(n - 1)\\n \\ncpdef int fact4(int n):\\n if n <= 1:\\n return 1\\n else:\\n return n * fact4(n - 1)')\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# %load save_test.py\nget_ipython().run_cell_magic('cython', '', '\\ncpdef int fact1(int n):\\n \"\"\"Doc\\n \\n Parameters\\n ----------\\n n : int\\n \\n \"\"\"\\n if (n <= 1):\\n return 1\\n \\n else:\\n return n * fact1(n - 1)\\n \\ndef fact2(n):\\n if (n <= 1):\\n return 1\\n else:\\n return n * fact2(n - 1)\\n \\ndef fact3(int n):\\n if (n <= 1):\\n return 1\\n else:\\n return n * fact3(n - 1)\\n \\ncpdef int fact4(int n):\\n if n <= 1:\\n return 1\\n else:\\n return n * fact4(n - 1)')\n", | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%save save_test.py 7", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "File `save_test.py` exists. Overwrite (y/[N])? y\nThe following commands were written to file `save_test.py`:\ndef fact_python(n):\n if (n <= 1):\n return 1\n else:\n return n * fact_python(n - 1)\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "# %load save_test.py\ndef fact_python(n):\n if (n <= 1):\n return 1\n else:\n return n * fact_python(n - 1)", | |
"execution_count": 15, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "import numpy as np", | |
"execution_count": 16, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "%%cython\nimport numpy as np\ncimport numpy as np\n\nDTYPE = np.float\nctypedef np.float_t DTYPE_t\n\ndef sum_by_row(np.ndarray[DTYPE_t, ndim=2] f):\n cdef int n_row = f.shape[0]\n cdef int n_col = f.shape[1]\n \n cdef np.ndarray[DTYPE_t, ndim=1] result = np.zeros([n_col], dtype=DTYPE)\n \n cdef int i\n cdef int j\n \n for i in range(n_row):\n for c in range(n_col):\n result[c] += f[i, c]\n\n return result", | |
"execution_count": 17, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%cython\nimport numpy as np\ncimport numpy as np\n\nDTYPE = np.float\nctypedef np.float_t DTYPE_t\n\ndef sum_by_row2(np.ndarray[DTYPE_t, ndim=2] f):\n cdef int n_row = f.shape[0]\n cdef int n_col = f.shape[1]\n \n cdef np.ndarray[DTYPE_t, ndim=1] result = np.zeros([n_col], dtype=DTYPE)\n \n cdef int i\n cdef int j\n \n for i in range(n_row):\n for c in range(n_col):\n result[c] += f[i, c]\n\n return result", | |
"execution_count": 18, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%cython\nimport numpy as np\ncimport numpy as np\n\ndef sum_by_row3(np.ndarray f):\n cdef int n_row = f.shape[0]\n cdef int n_col = f.shape[1]\n \n cdef np.ndarray result = np.zeros([n_col])\n \n cdef int i\n cdef int j\n \n for i in range(n_row):\n for c in range(n_col):\n result[c] += f[i, c]\n\n return result", | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "A = np.random.randn(32, 32)", | |
"execution_count": 20, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 10000 sum_by_row(A)", | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "3.06 µs ± 180 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 10000 A.sum(0)", | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "3.41 µs ± 190 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 10000 sum_by_row2(A)", | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "3.53 µs ± 222 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 10000 sum_by_row3(A)", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "220 µs ± 8.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "def sum_by_row_python(f):\n n_row = f.shape[0]\n n_col = f.shape[1]\n \n result = np.zeros([n_col])\n \n \n for i in range(n_row):\n for c in range(n_col):\n result[c] += f[i, c]\n\n return result", | |
"execution_count": 25, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%timeit -n 1000 sum_by_row_python(A)", | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "355 µs ± 14.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.1", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "0993e87adb7d0332a843c32f35dd8145", | |
"data": { | |
"description": "jupyter_magic.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/0993e87adb7d0332a843c32f35dd8145" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment