Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tillahoffmann/832663099ec52edc1a92fa9154955843 to your computer and use it in GitHub Desktop.
Save tillahoffmann/832663099ec52edc1a92fa9154955843 to your computer and use it in GitHub Desktop.
Trying to profile cython instance methods line by line
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The cython extension is already loaded. To reload it, use:\n",
" %reload_ext cython\n"
]
}
],
"source": [
"%load_ext cython\n",
"import line_profiler"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%cython\n",
"# distutils: define_macros=CYTHON_TRACE_NOGIL=1\n",
"# cython: linetrace=True\n",
"# cython: binding=True\n",
"\n",
"def cumulative_sum(int n):\n",
" cdef int s=0, i\n",
" for i in range(n):\n",
" s += i\n",
" \n",
" return s\n",
"\n",
"\n",
"cdef class Test(object):\n",
" def cumulative_sum(self, int n):\n",
" cdef int s=0, i\n",
" for i in range(n):\n",
" s += i\n",
"\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Timer unit: 1e-06 s\n",
"\n",
"Total time: 2.9e-05 s\n",
"File: /Users/tillhoffmann/.ipython/cython/_cython_magic_3fe78af381a9aa1dacad9dc0e5829b44.pyx\n",
"Function: cumulative_sum at line 5\n",
"\n",
"Line # Hits Time Per Hit % Time Line Contents\n",
"==============================================================\n",
" 5 def cumulative_sum(int n):\n",
" 6 1 1 1.0 3.4 cdef int s=0, i\n",
" 7 1 1 1.0 3.4 for i in range(n):\n",
" 8 100 27 0.3 93.1 s += i\n",
" 9 \n",
" 10 1 0 0.0 0.0 return s\n",
"\n"
]
}
],
"source": [
"#Print profiling statistics using the `line_profiler` API\n",
"profile = line_profiler.LineProfiler(cumulative_sum)\n",
"profile.runcall(cumulative_sum, 100)\n",
"profile.print_stats()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Timer unit: 1e-06 s\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python2.7/site-packages/ipykernel/__main__.py:3: UserWarning: Could not extract a code object for the object <built-in method cumulative_sum of _cython_magic_3fe78af381a9aa1dacad9dc0e5829b44.Test object at 0x1068b66d0>\n",
" app.launch_new_instance()\n"
]
}
],
"source": [
"#Print profiling statistics using the `line_profiler` API\n",
"test = Test()\n",
"profile = line_profiler.LineProfiler(test.cumulative_sum)\n",
"profile.runcall(test.cumulative_sum, 100)\n",
"profile.print_stats()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hasattr(cumulative_sum, '__code__'): True\n",
"hasattr(test.cumulative_sum, '__code__'): False\n"
]
}
],
"source": [
"print \"hasattr(cumulative_sum, '__code__'): {}\".format(hasattr(cumulative_sum, '__code__'))\n",
"print \"hasattr(test.cumulative_sum, '__code__'): {}\".format(hasattr(test.cumulative_sum, '__code__'))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@tbrandvik
Copy link

I was wondering if you have had any luck getting this to work since you posted it? I'm having the exact same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment