开发者

How to remove python assertion when compiling in cython?

so, here is my problem: I code in python, but I need to improve performance in some part of my code that are too slow. A good(and easy) solution seems to be using cython; I tried it and got good results. The issue is that I use assert statement in my python co开发者_Go百科de. Before using cython, I could compile my python code with the -OO option, so that I can deliver a version not executing any assertion test, and still have the assert for debug. But the files that are compiled in cython seems to always execute the asserts. Is there some options that can be passed to cython compilation to remove(or not remove) the assertions?


Cython skips the assertions if you define the C preprocessor macro PYREX_WITHOUT_ASSERTIONS. So pass -DPYREX_WITHOUT_ASSERTIONS to the C compiler when compiling the generated C file. How to to this depends on your build process.


Use pypreprocessor

Which can also be found on PYPI (Python Package Index) and be fetched using pip.

Here's the implementation:

from pypreprocessor import pypreprocessor

pypreprocessor.parse()

#define debug

#ifdef debug
...place assert to be removed here...
#endif

This essentially works the same as the standard C preprocessor conditional compilation does.

SideNote: This module is compatible with both python2x and python3k.

Disclaimer: I'm the author of pypreprocessor.

This will make the initial load take longer due to the added preprocessor stage but the outputted bytecode (.pyc) will be optimized.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜