Skip to content

Instantly share code, notes, and snippets.

@nmayorov
Created November 16, 2016 11:36
Show Gist options
  • Save nmayorov/42c5cee8a8a6cf3b27e2a0537f9ce62d to your computer and use it in GitHub Desktop.
Save nmayorov/42c5cee8a8a6cf3b27e2a0537f9ce62d to your computer and use it in GitHub Desktop.
Profile with Cython complied function in ODE solvers
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from scipy.integrate import solve_ivp"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%load_ext Cython"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def fun(t, y):\n",
" mu = 50\n",
" return np.array([y[1], mu * (1 - y[0] ** 2) * y[1] - y[0]])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def jac(t, y):\n",
" mu = 50\n",
" return np.array([\n",
" [0, 1],\n",
" [-2 * mu * y[0] * y[1] - 1, mu * (1 - y[0] ** 2)]])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cython\n",
"def fun_cython(double t, double[:] y, double [:] f):\n",
" cdef double mu = 50\n",
" f[0] = y[1]\n",
" f[1] = mu * (1 - y[0] * y[0]) * y[1] - y[0]\n",
" return f.base.copy()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"f_cython = np.empty(2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"y0 = [2, 0]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" "
]
}
],
"source": [
"stats = %prun -r -q solve_ivp(fun, [0, 1000], y0)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 2168754 function calls in 3.306 seconds\n",
"\n",
" Ordered by: internal time\n",
" List reduced from 42 to 20 due to restriction <20>\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 34082 0.941 0.000 2.321 0.000 rk.py:15(rk_step)\n",
" 27354 0.452 0.000 3.097 0.000 rk.py:104(_step_impl)\n",
" 204494 0.434 0.000 0.745 0.000 <ipython-input-4-54601b8d6733>:1(fun)\n",
" 470432 0.346 0.000 0.346 0.000 {built-in method numpy.core.multiarray.array}\n",
" 272659 0.241 0.000 0.241 0.000 {built-in method numpy.core.multiarray.dot}\n",
" 204494 0.147 0.000 1.043 0.000 base.py:11(fun_wrapped)\n",
" 238581 0.146 0.000 0.176 0.000 numeric.py:414(asarray)\n",
" 204494 0.132 0.000 1.175 0.000 base.py:124(fun)\n",
" 34085 0.125 0.000 0.246 0.000 linalg.py:1976(norm)\n",
" 27354 0.055 0.000 3.152 0.000 base.py:147(step)\n",
" 1 0.052 0.052 3.306 3.306 ivp.py:153(solve_ivp)\n",
" 34085 0.047 0.000 0.293 0.000 common.py:38(norm)\n",
" 27355 0.043 0.000 0.071 0.000 shape_base.py:61(atleast_2d)\n",
" 34085 0.034 0.000 0.034 0.000 {method 'ravel' of 'numpy.ndarray' objects}\n",
" 34083 0.020 0.000 0.020 0.000 {built-in method builtins.max}\n",
" 27356 0.015 0.000 0.018 0.000 numeric.py:484(asanyarray)\n",
" 34085 0.013 0.000 0.019 0.000 linalg.py:111(isComplexType)\n",
" 68170 0.013 0.000 0.013 0.000 {built-in method builtins.issubclass}\n",
" 27355 0.010 0.000 0.010 0.000 {built-in method builtins.min}\n",
" 82063 0.010 0.000 0.010 0.000 {method 'append' of 'list' objects}\n",
"\n",
"\n"
]
},
{
"data": {
"text/plain": [
"<pstats.Stats at 0x108c71860>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stats.print_stats(20)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" "
]
}
],
"source": [
"stats = %prun -r -q solve_ivp(lambda t, y: fun_cython(t, y, f_cython), [0, 1000], y0)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 2168166 function calls in 3.815 seconds\n",
"\n",
" Ordered by: internal time\n",
" List reduced from 43 to 20 due to restriction <20>\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 34072 1.226 0.000 2.663 0.000 rk.py:15(rk_step)\n",
" 204434 0.685 0.000 0.685 0.000 {_cython_magic_da45c9fac9fe52f051fb8b16785cf918.fun_cython}\n",
" 27350 0.543 0.000 3.570 0.000 rk.py:104(_step_impl)\n",
" 272579 0.256 0.000 0.256 0.000 {built-in method numpy.core.multiarray.dot}\n",
" 238511 0.165 0.000 0.199 0.000 numeric.py:414(asarray)\n",
" 204434 0.159 0.000 1.084 0.000 base.py:11(fun_wrapped)\n",
" 34075 0.139 0.000 0.277 0.000 linalg.py:1976(norm)\n",
" 204434 0.139 0.000 1.223 0.000 base.py:124(fun)\n",
" 1 0.076 0.076 3.815 3.815 ivp.py:153(solve_ivp)\n",
" 204434 0.070 0.000 0.755 0.000 <string>:1(<lambda>)\n",
" 27350 0.067 0.000 3.637 0.000 base.py:147(step)\n",
" 34075 0.056 0.000 0.333 0.000 common.py:38(norm)\n",
" 27351 0.044 0.000 0.071 0.000 shape_base.py:61(atleast_2d)\n",
" 34075 0.040 0.000 0.040 0.000 {method 'ravel' of 'numpy.ndarray' objects}\n",
" 265864 0.039 0.000 0.039 0.000 {built-in method numpy.core.multiarray.array}\n",
" 34073 0.021 0.000 0.021 0.000 {built-in method builtins.max}\n",
" 27352 0.015 0.000 0.018 0.000 numeric.py:484(asanyarray)\n",
" 34075 0.014 0.000 0.019 0.000 linalg.py:111(isComplexType)\n",
" 68150 0.014 0.000 0.014 0.000 {built-in method builtins.issubclass}\n",
" 27351 0.010 0.000 0.010 0.000 {built-in method builtins.min}\n",
"\n",
"\n"
]
},
{
"data": {
"text/plain": [
"<pstats.Stats at 0x108c2b9e8>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stats.print_stats(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment