Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save den-run-ai/7727874c4fe1e7e174ed953930e93bbc to your computer and use it in GitHub Desktop.
Save den-run-ai/7727874c4fe1e7e174ed953930e93bbc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%load_ext cython"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'0.24'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import cython\n",
"cython.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('0.24.0', '1.10.4')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numba as nb\n",
"import numpy as np\n",
"nb.__version__, np.__version__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cython\n",
"cpdef long cy_sum(long[:] A):\n",
" cdef long i, n = A.shape[0], s = 0\n",
" for i in range(n):\n",
" s += A[i]\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"@nb.jit(nopython=True)\n",
"def sumr(i):\n",
" s=0\n",
" for x in range(i):\n",
" s+=x\n",
" return s\n",
"\n",
"r1=sum(range(int(1e6)))\n",
"r2=sumr(int(1e6))\n",
"r3=np.sum(np.arange(0,int(1e6)))\n",
"r4=cy_sum(np.arange(0,int(1e6)))\n",
"print(r2==r1)\n",
"print(r3==r2)\n",
"print(r4==r3)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 loops, best of 3: 38.8 ms per loop\n"
]
}
],
"source": [
"%timeit sum(range(int(1e6)))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 13.38 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 697 ns per loop\n"
]
}
],
"source": [
"%timeit sumr(int(1e6))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 3.04 ms per loop\n"
]
}
],
"source": [
"%timeit np.sum(np.arange(0,int(1e6)))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 2.98 ms per loop\n"
]
}
],
"source": [
"%timeit cy_sum(np.arange(0,int(1e6)))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/dta/anaconda3/lib/python3.5/site-packages/IPython/utils/path.py:264: UserWarning: get_ipython_cache_dir has moved to the IPython.paths module\n",
" warn(\"get_ipython_cache_dir has moved to the IPython.paths module\")\n"
]
},
{
"data": {
"application/javascript": [
"$.getScript(\"https://raw.github.com/marijnh/CodeMirror/master/mode/fortran/fortran.js\", function () {\n",
"IPython.config.cell_magic_highlight['magic_fortran'] = {'reg':[/^%%fortran/]};});\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%load_ext fortranmagic"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gnu95FCompiler instance properties:\r\n",
" archiver = ['/usr/bin/gfortran', '-cr']\r\n",
" compile_switch = '-c'\r\n",
" compiler_f77 = ['/usr/bin/gfortran', '-Wall', '-ffixed-form', '-fno-\r\n",
" second-underscore', '-fPIC', '-O3', '-funroll-loops']\r\n",
" compiler_f90 = ['/usr/bin/gfortran', '-Wall', '-fno-second-underscore',\r\n",
" '-fPIC', '-O3', '-funroll-loops']\r\n",
" compiler_fix = ['/usr/bin/gfortran', '-Wall', '-ffixed-form', '-fno-\r\n",
" second-underscore', '-Wall', '-fno-second-underscore', '-\r\n",
" fPIC', '-O3', '-funroll-loops']\r\n",
" libraries = ['gfortran']\r\n",
" library_dirs = []\r\n",
" linker_exe = ['/usr/bin/gfortran', '-Wall', '-Wall']\r\n",
" linker_so = ['/usr/bin/gfortran', '-Wall', '-Wall', '-shared']\r\n",
" object_switch = '-o '\r\n",
" ranlib = ['/usr/bin/gfortran']\r\n",
" version = LooseVersion ('4.8.4-2')\r\n",
" version_cmd = ['/usr/bin/gfortran', '--version']\r\n",
"Fortran compilers found:\r\n",
" --fcompiler=gnu95 GNU Fortran 95 compiler (4.8.4-2)\r\n",
"Compilers available for this platform, but not found:\r\n",
" --fcompiler=absoft Absoft Corp Fortran Compiler\r\n",
" --fcompiler=compaq Compaq Fortran Compiler\r\n",
" --fcompiler=g95 G95 Fortran Compiler\r\n",
" --fcompiler=gnu GNU Fortran 77 compiler\r\n",
" --fcompiler=intel Intel Fortran Compiler for 32-bit apps\r\n",
" --fcompiler=intele Intel Fortran Compiler for Itanium apps\r\n",
" --fcompiler=intelem Intel Fortran Compiler for 64-bit apps\r\n",
" --fcompiler=lahey Lahey/Fujitsu Fortran 95 Compiler\r\n",
" --fcompiler=nag NAGWare Fortran 95 Compiler\r\n",
" --fcompiler=pathf95 PathScale Fortran Compiler\r\n",
" --fcompiler=pg Portland Group Fortran Compiler\r\n",
" --fcompiler=vast Pacific-Sierra Research Fortran 90 Compiler\r\n",
"Compilers not available on this platform:\r\n",
" --fcompiler=hpux HP Fortran 90 Compiler\r\n",
" --fcompiler=ibm IBM XL Fortran Compiler\r\n",
" --fcompiler=intelev Intel Visual Fortran Compiler for Itanium apps\r\n",
" --fcompiler=intelv Intel Visual Fortran Compiler for 32-bit apps\r\n",
" --fcompiler=intelvem Intel Visual Fortran Compiler for 64-bit apps\r\n",
" --fcompiler=mips MIPSpro Fortran Compiler\r\n",
" --fcompiler=none Fake Fortran compiler\r\n",
" --fcompiler=sun Sun or Forte Fortran 95 Compiler\r\n",
"For compiler details, run 'config_fc --verbose' setup command.\r\n",
"Removing build directory /tmp/tmp6dR0vM\r\n"
]
}
],
"source": [
"!f2py -c --help-fcompiler"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%fortran\n",
"\n",
" subroutine sumf(X, n, z)\n",
" integer*8, intent(in) :: X(*)\n",
" integer*8, intent(in) :: n\n",
" integer*8, intent(out) :: z\n",
" z = 0\n",
" DO 20 J = 1, n\n",
" z = z + X(J)\n",
"20 continue\n",
" end subroutine sumf"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 2.95 ms per loop\n"
]
}
],
"source": [
"%timeit sumf(np.arange(0,int(1e6)),int(1e6))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(499999500000, 499999500000)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sumf(np.arange(0,int(1e6)),int(1e6)), r4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment