Created
September 16, 2021 19:15
-
-
Save simecek/8c614428d435baa375d29e6faac8bd1e to your computer and use it in GitHub Desktop.
Cython_demo.ipynb
This file contains hidden or 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Cython_demo.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyOEtHt6TccOx9FlT5Dfiyrl", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/simecek/8c614428d435baa375d29e6faac8bd1e/cython_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "p4eL8n3H9tmN" | |
}, | |
"source": [ | |
"%load_ext cython" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "jWcwRAKRBvD3" | |
}, | |
"source": [ | |
"## Python" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6uL79B9v-WrQ" | |
}, | |
"source": [ | |
"def f_py(x):\n", | |
" return x**2 - x\n", | |
"\n", | |
"def intg_py(a, b, N):\n", | |
" s = 0\n", | |
" dx = (b - a) / N\n", | |
" for i in range(N):\n", | |
" s += f_py(a + i*dx)\n", | |
" return s * dx" | |
], | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "rMyx9oW_-16_", | |
"outputId": "b25bf588-ff46-4ef6-e75f-7dbeae2c54c1" | |
}, | |
"source": [ | |
"%%time\n", | |
"intg_py(0, 10, 1000000)" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"CPU times: user 268 ms, sys: 0 ns, total: 268 ms\n", | |
"Wall time: 270 ms\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"283.3328833334909" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "o2z07MW7By7r" | |
}, | |
"source": [ | |
"## Cython (same as .py)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "znP_cZPR-d45" | |
}, | |
"source": [ | |
"%%cython\n", | |
"\n", | |
"def f_c1(x):\n", | |
" return x**2 - x\n", | |
"\n", | |
"def intg_c1(a, b, N):\n", | |
" s = 0\n", | |
" dx = (b - a) / N\n", | |
" for i in range(N):\n", | |
" s += f_c1(a + i*dx)\n", | |
" return s * dx" | |
], | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "IYx6UA97Ba--", | |
"outputId": "3b115cbe-d8d9-4c4f-9eaf-b0c82d42c85e" | |
}, | |
"source": [ | |
"%%time\n", | |
"intg_c1(0, 10, 1000000)" | |
], | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"CPU times: user 137 ms, sys: 0 ns, total: 137 ms\n", | |
"Wall time: 137 ms\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"283.3328833334909" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "t_hasicpCMQh" | |
}, | |
"source": [ | |
"## Cython (added static typing, cdef)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-_RJ8bTu-ieN" | |
}, | |
"source": [ | |
"%%cython\n", | |
"\n", | |
"cdef double f_c2(double x):\n", | |
" return x**2 - x\n", | |
"\n", | |
"def intg_c2(double a, double b, int N):\n", | |
" cdef int i\n", | |
" cdef double s, dx\n", | |
" s = 0\n", | |
" dx = (b - a) / N\n", | |
" for i in range(N):\n", | |
" s += f_c2(a + i*dx)\n", | |
" return s * dx" | |
], | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "F-1KZpe9BW-S", | |
"outputId": "158e68e8-cfb8-4ef7-b7c7-897f9cd73d88" | |
}, | |
"source": [ | |
"%%time\n", | |
"intg_c2(0, 10, 1000000)" | |
], | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"CPU times: user 1.18 ms, sys: 0 ns, total: 1.18 ms\n", | |
"Wall time: 1.19 ms\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"283.3328833334909" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "hREKsO5BBilA" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment