在cython中编译时如何删除python断言? [英] How to remove python assertion when compiling in cython?

查看:125
本文介绍了在cython中编译时如何删除python断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的问题: 我使用python编写代码,但是我需要在代码的某些太慢的地方提高性能.好的(简便)解决方案似乎是在使用cython.我尝试了一下,并取得了良好的效果. 问题是我在python代码中使用assert语句.在使用cython之前,我可以使用-OO选项编译我的python代码,这样我就可以提供一个不执行任何断言测试的版本,并且仍然具有要进行调试的断言.但是用cython编译的文件似乎总是执行断言.是否可以将某些选项传递给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 code. 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?

推荐答案

如果定义C预处理程序宏PYREX_WITHOUT_ASSERTIONS,Cython将跳过断言.因此,在编译生成的C文件时,将-DPYREX_WITHOUT_ASSERTIONS传递给C编译器.如何执行此操作取决于您的构建过程.

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.

这篇关于在cython中编译时如何删除python断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆