[Tutor] improving speed using and recalling C functions

Albert-Jan Roskam fomcl at yahoo.com
Fri Apr 11 14:16:12 CEST 2014


________________________________
> From: Gabriele Brambilla <gb.gabrielebrambilla at gmail.com>
>To: Danny Yoo <dyoo at hashcollision.org> 
>Cc: python tutor <tutor at python.org> 
>Sent: Friday, April 11, 2014 5:30 AM
>Subject: Re: [Tutor] improving speed using and recalling C functions
>  
>
>
>Hi Danny,
>I followed your suggestion.
>Tomorrow morning I will run this new version of the code.
>
>
>Now using a sample of 81 elements (instead of 600000) the profile returns: 
>
>
>Thu Apr 10 23:25:59 2014    restats
>
>
>         18101188 function calls in 1218.626 seconds
>
>
>   Ordered by: internal time
>   List reduced from 13 to 10 due to restriction <10> 
>
>
>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>        1 1015.803 1015.803 1218.334 1218.334 skymaps5.py:44(mymain)
> 18101000  202.490    0.000  202.490    0.000 {method 'write' of 'file' objects} 
>
>
>        1    0.292    0.292 1218.626 1218.626 <string>:1(<module>)
>        6    0.029    0.005    0.029    0.005 {open}
>        5    0.010    0.002    0.010    0.002 {method 'close' of 'file' objects} 
>
>
>       81    0.002    0.000    0.002    0.000 {method 'split' of 'str' objects}
>       82    0.001    0.000    0.001    0.000 {zip}
>        1    0.000    0.000    0.000    0.000 function_base.py:8(linspace) 
>        1    0.000    0.000    0.000    0.000 function_base.py:93(logspace)
>        5    0.000    0.000    0.000    0.000 {numpy.core.multiarray.zeros}
>
>
>Anyway I would like to try to speed it up using C functions (and maybe comparing the resuts of the two profile in the end) 
>How can I do it now? Can I use Cython?

If you have a compiler installed already it's just easy_install cython. Writing Cython is not hard. That is, you easily get speed improvements. It's in a .pyx file and once you're done you generate the .c and .so/.dll files with a setup.py like below. Reason why I am posting this snippet is the way to generate an annotated html file of your cython code. The whiter, the more in C, the better. Yellow means stuff might still be improved more.
 
* setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import Cython.Compiler.Options
Cython.Compiler.Options.annotate = True   # <---- really handy

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("myModule", ["myModule.pyx"])]

)


More information about the Tutor mailing list