http://www.lptms.universite-paris-saclay.fr//wiki/index.php?title=Scientific_Programming_with_Python_(for_the_debug),_and_C(ython)_for_the_speed&feed=atom&action=historyScientific Programming with Python (for the debug), and C(ython) for the speed - Revision history2024-03-29T02:29:04ZRevision history for this page on the wikiMediaWiki 1.39.4http://www.lptms.universite-paris-saclay.fr//wiki/index.php?title=Scientific_Programming_with_Python_(for_the_debug),_and_C(ython)_for_the_speed&diff=403&oldid=prevLandes at 14:39, 14 November 20132013-11-14T14:39:17Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 15:39, 14 November 2013</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l3">Line 3:</td>
<td colspan="2" class="diff-lineno">Line 3:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There is actually a [http://docs.cython.org/src/userguide/numpy_tutorial.html tutorial for Numpy users] which is pretty well done. Here the main steps of the cythonization process are summarized, as a teaser-guide.</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There is actually a [http://docs.cython.org/src/userguide/numpy_tutorial.html tutorial for Numpy users] which is pretty well done. Here the main steps of the cythonization process are summarized, as a teaser-guide.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If you are not convinced by the final speed one can obtain with Cython, there are some benchmarks on the web, and also [http://docs.cython.org/src/tutorial/profiling_tutorial.html Cython profiling tutorial].</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If you are not convinced by the final speed one can obtain with Cython, there are some <ins style="font-weight: bold; text-decoration: none;">[http://technicaldiscovery.blogspot.com.ar/2011/06/speeding-up-python-numpy-cython-and.html </ins>benchmarks<ins style="font-weight: bold; text-decoration: none;">] </ins>on the web, and also <ins style="font-weight: bold; text-decoration: none;">the </ins>[http://docs.cython.org/src/tutorial/profiling_tutorial.html Cython profiling tutorial].</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>The methodology is the <del style="font-weight: bold; text-decoration: none;">following</del>:</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>The methodology is <ins style="font-weight: bold; text-decoration: none;">outlined here, with a focus on </ins>the <ins style="font-weight: bold; text-decoration: none;">important details</ins>:</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>- Write your code in pure Python. It is very easy, so you can play with it, think about the best algorithm to implement your problem, etc. Use iPython for debugging (you can access and modify variables at run time after an error occurred).</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>- Write your code in pure Python. It is very easy, so you can play with it, think about the best algorithm to implement your problem, etc. Use iPython for debugging (you can access and modify variables at run time after an error occurred).</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l24">Line 24:</td>
<td colspan="2" class="diff-lineno">Line 24:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>- Type all the core variables, especially the arrays you use. You can use profiling tools, but first you should try to take off all the API calls you can. When there is no more yellow in the main loop of your code, you should be fine, if your algorithm (independently form the language) is well designed.</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>- Type all the core variables, especially the arrays you use. You can use profiling tools, but first you should try to take off all the API calls you can. When there is no more yellow in the main loop of your code, you should be fine, if your algorithm (independently form the language) is well designed.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>- Deactivate the boundscheck and wraparound when debugging is over. </div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>- Deactivate the boundscheck and wraparound when debugging is over<ins style="font-weight: bold; text-decoration: none;">: Add </ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">\#cython: boundscheck=False </ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">\#cython: wraparound=False</ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">in your code, before any code or whitespace (but there can be some comments before)</ins>.</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Your code is now almost as fast as C, and you saved a lot of debugging time ! Since Cython compiles the code with the --fno-strict-aliasing option, performance is not very far from Fortran either.</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Your code is now almost as fast as C, and you saved a lot of debugging time ! <ins style="font-weight: bold; text-decoration: none;">The sytax is that of Python, except for the declarations of variables (using cdef). I at some point you need to go back to a debugging-friendly environment, you can always copy-paste your pyx file in .py file, comment all the declarations, and work with the python code. </ins></div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> </div></td></tr>
<tr><td colspan="2" class="diff-side-deleted"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Since Cython compiles the code with the --fno-strict-aliasing option, performance is not very far from Fortran either.</div></td></tr>
</table>Landeshttp://www.lptms.universite-paris-saclay.fr//wiki/index.php?title=Scientific_Programming_with_Python_(for_the_debug),_and_C(ython)_for_the_speed&diff=401&oldid=prevLandes: Created page with "With Cython, one can finally use Python and obtain near C-like performance. This is especially valuable if you have to start a project from scratch. There is actually a [http..."2013-11-14T14:12:23Z<p>Created page with "With Cython, one can finally use Python and obtain near C-like performance. This is especially valuable if you have to start a project from scratch. There is actually a [http..."</p>
<p><b>New page</b></p><div>With Cython, one can finally use Python and obtain near C-like performance. This is especially valuable if you have to start a project from scratch.<br />
<br />
There is actually a [http://docs.cython.org/src/userguide/numpy_tutorial.html tutorial for Numpy users] which is pretty well done. Here the main steps of the cythonization process are summarized, as a teaser-guide.<br />
<br />
If you are not convinced by the final speed one can obtain with Cython, there are some benchmarks on the web, and also [http://docs.cython.org/src/tutorial/profiling_tutorial.html Cython profiling tutorial].<br />
<br />
<br />
The methodology is the following:<br />
<br />
- Write your code in pure Python. It is very easy, so you can play with it, think about the best algorithm to implement your problem, etc. Use iPython for debugging (you can access and modify variables at run time after an error occurred).<br />
<br />
- Remove all the use of Python-specific data containers like sets, dicts, lists. Instead, use numpy arrays. <br />
<br />
- Do not use vectorial operations, instead, use loops, as you would do in C/C++. This is highly UN-efficient in Numpy, but it is what you need for Cython.<br />
<br />
Your code is now very slow, because you use loops in Python/Numpy, which is deprecated, and because the Python is overloaded with tons of background checks, and allows dynamic typing (more checks to be done at execution time).<br />
<br />
- Cythonize it ! You have to create a setup.py file, copy-paste the bulk of your original code in some main() function of some .pyx file. (This is explained very well [http://docs.cython.org/0.15/src/userguide/tutorial.html Cython Hello World]. (Hint: to indent a block of text, you can usually select it (even hundreds of lines) and hit Tab, e.g. in gedit).<br />
<br />
Your code is now compiled in Cython, wrapped in some main() function (plus additional functions if you have some). It is still very slow, because the .c file created does all the usual Python checks. <br />
<br />
- Do $ cython -a your_pyx_file.pyx . Open the html document produced: the yellow indicates how much the Python API is called. You can click on each line to see the C implementation and the details of the PyAPI calls.<br />
<br />
- Type all the core variables, especially the arrays you use. You can use profiling tools, but first you should try to take off all the API calls you can. When there is no more yellow in the main loop of your code, you should be fine, if your algorithm (independently form the language) is well designed.<br />
<br />
- Deactivate the boundscheck and wraparound when debugging is over. <br />
<br />
Your code is now almost as fast as C, and you saved a lot of debugging time ! Since Cython compiles the code with the --fno-strict-aliasing option, performance is not very far from Fortran either.</div>Landes