Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one overwrite the default compile flags for Cython when building with distutils?

I am compiling some cython extensions in linux and noticed that it defaults to using -O2 when building from the distutils mechanism. I was wondering if there was a simple way to change that to a -O3.

I have tried using the extra_compile_args on Extension objects, but that leads to both -O2 and -O3 being passed as arguments to gcc. I kind of want to play with other esoteric gcc options and thus am hoping I can just control the compilation step. An obvious question is "why don't I just run cython my.pyx and compile the results manually?". I would love to, is my answer... but the cython executable in /usr/local/bin/ throws a DistributionNotFound: Cython==0.12.1 error when run from the command line. I haven't quite figured that one out.

Anyway, I am not sure if its a cython thing, a distutils thing or a broken apt package thing. I simply grabbed cython out of the ubuntu 11.10 apt repo (and am currently using ubuntu 11.10).

like image 934
Voltaire Avatar asked Nov 23 '11 02:11

Voltaire


People also ask

Does Cython need to be compiled?

Cython source file names consist of the name of the module followed by a . pyx extension, for example a module called primes would have a source file named primes. pyx . Cython code, unlike Python, must be compiled.

Does Cython compile to C?

The Cython language is a superset of Python that compiles to C, yielding performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python's native object types, the speedups won't be large.


2 Answers

Using extra_compile_args=["-O3"] in your setup.py, the "-O3" should appear after the "-O2" option overrading it. Check the share object (.so, or .dll) size in order to confirm it quickly.

Davide

like image 184
Davide Albanese Avatar answered Dec 08 '22 00:12

Davide Albanese


larsmans comment was right - using /usr/bin/cython addresses my issue.

like image 37
Voltaire Avatar answered Dec 08 '22 01:12

Voltaire