【问题标题】:Extending python - to swig, not to swig or Cython扩展 python - swig,而不是 swig 或 Cython
【发布时间】:2010-10-02 04:41:56
【问题描述】:

我在我的 python 代码中发现了瓶颈,玩弄了 Psycho 等。然后决定编写一个 c/c++ 扩展来提高性能。

在 swig 的帮助下,您几乎不需要关心参数等。一切正常。

现在我的问题是:swig 创建了一个相当大的 py 文件,它在调用实际的 .pyd 或 .so 代码之前会执行大量“检查”和“PySwigObject”。

有没有人有任何经验,如果你手写这个文件或者让 swig 来做,是否可以获得更多的性能。

【问题讨论】:

    标签: python c++ c swig cython


    【解决方案1】:

    如果您不打算使用 swig 为其他语言生成绑定,则应该考虑使用 Boost.Python。

    如果您有很多函数和类要绑定,Py++ 是一个很好的工具,它可以自动生成所需的代码来进行绑定。

    Pybindgen 也可能是一个选项,但它是一个新项目,不如 Boost.Python 完整。


    编辑:

    也许我需要更明确地说明利弊。

    • 痛饮:

      专业人士:您可以为多种脚本语言生成绑定。

      缺点:我不喜欢解析器的工作方式。我不知道是否取得了一些进展,但两年前 C++ 解析器非常有限。大多数时候,我不得不复制/粘贴我的 .h 标头,添加一些 % 字符并为 swig 解析器提供额外的提示。

      我还需要不时处理 Python C-API 以进行(不是那么)复杂的类型转换。

      我不再使用它了。

    • Boost.Python:

      专业人士: 这是一个非常完整的图书馆。它允许您使用 C-API 做几乎所有可能的事情,但在 C++ 中。我从来不需要用这个库编写 C-API 代码。由于图书馆,我也从未遇到过错误。绑定代码要么像魅力一样工作,要么拒绝编译。

      如果您已经有一些 C++ 库要绑定,它可能是目前可用的最佳解决方案之一。但如果你只有一个小的 C 函数要重写,我可能会尝试使用 Cython。

      缺点:如果您没有预编译的 Boost.Python 库,您将使用 Bjam(类似于 make 替换)。我真的很讨厌 Bjam 及其语法。

      使用 B.P 创建的 Python 库往往会变得肥胖。编译它们也需要很多时间。

    • Py++(已停产):让 Boost.Python 变得简单。 Py++ 使用 C++ 解析器来读取您的代码,然后自动生成 Boost.Python 代码。您也得到了作者的大力支持(不,不是我 ;-))。

      缺点:只有 Boost.Python 本身的问题。更新:截至 2014 年,该项目现在看起来已停止。

    • Pybindgen:

      它生成处理 C-API 的代码。你可以在 Python 文件中描述函数和类,或者让 Pybindgen 读取你的头文件并自动生成绑定(为此它使用 pygccxml,一个由 Py++ 的作者编写的 Python 库)。

      缺点:这是一个年轻的项目,团队比 Boost.Python 小。仍然有一些限制:您不能对 C++ 类使用多重继承,回调(不是自动的,可以编写自定义回调处理代码)。将 Python 异常翻译成 C。

      绝对值得一看。

    • 一个新的: 在 2009 年 1 月 20 日,Py++ 的作者宣布了一个new package 用于将 C/C++ 代码与 python 接口。它基于 ctypes。我还没有尝试过,但我会的!注意:这个项目看起来像 Py++ 一样中断。

    • CFFI: 直到最近我才知道这个的存在所以现在我不能给出我的意见。看起来您可以在 Python 字符串中定义 C 函数并直接从同一个 Python 模块中调用它们。

    • Cython:这是我目前在项目中使用的方法。基本上你在特殊的 .pyx 文件中编写代码。这些文件被编译(翻译)成 C 代码,而 C 代码又被编译成 CPython 模块。 Cython 代码可能看起来像常规 Python(实际上纯 Python 是有效的 .pyx Cython 文件),但您还可以提供更多信息,例如变量类型。这种可选类型允许 Cython 生成更快的 C 代码。 Cython 文件中的代码既可以调用纯 Python 函数,也可以调用 C 和 C++ 函数(以及 C++ 方法)。

      我花了一些时间在 Cython 中思考,在同一代码中调用 C 和 C++ 函数,混合 Python 和 C 变量等等。但它是一种非常强大的语言,拥有一个活跃的(2014 年)和友好的社区。​​p>

    【讨论】:

    • 谢谢,swig 在过去几年中得到了发展。你只需 %include 你的 .h 文件,一切就完成了(又一个小时支持 unicode,你真的完成了 :-) 我的代码与 swig 配合得很好——我的问题是是否值得手动检查生成的 .py 代码所有的 pySwigObjects ...
    • 这些对象增加了一些开销。 Pybindgen 可能会为您的模块生成更简洁的 C 代码。我玩了一下它,实际上它生成的代码非常接近我手动完成的代码。
    【解决方案2】:

    SWIG 2.0.4 引入了一个新的 -builtin 选项,可提高性能。 我使用一个对 C++ 扩展进行大量快速调用的示例程序进行了一些基准测试。 我使用 boost.python、PyBindGen、SIP 和 SWIG 构建了扩展,带有和不带有 -builtin 选项。以下是结果(平均 100 次运行):

    SWIG with -builtin     2.67s
    SIP                    2.70s
    PyBindGen              2.74s
    boost.python           3.07s
    SWIG without -builtin  4.65s
    

    SWIG 曾经是最慢的。使用新的 -builtin 选项,SWIG 似乎是最快的。

    【讨论】:

    • 您能否就此展开更多说明,尤其是您转换为 C API 的基准测试代码。
    【解决方案3】:

    当然,手动执行此操作总会获得性能提升,但与执行此操作所需的努力相比,增益将非常小。我没有任何图可以给你,但我不建议这样做,因为你需要手动维护界面,如果你的模块很大,这不是一个选项!

    您选择使用脚本语言是正确的,因为您想要快速开发。这样你就避免了早期优化综合症,现在你想优化瓶颈部分,太棒了!但是如果你手动做 C/python 接口,你肯定会陷入早期优化综合症。

    如果你想要一些接口代码更少的东西,你可以考虑从你的 C 代码中创建一个 dll,然后通过 cstruct 直接从 python 中使用该库。

    如果您只想在程序中使用 python 代码,也可以考虑Cython

    【讨论】:

    • 你打错了 cython。如果您注意 cython 的工作原理,将 python 函数转换为 cython 可能是一个非常快速的过程。我已经有 30 倍的加速,而无需付出太多额外的努力。
    • “当然,手动执行此操作总会获得性能提升”——使用现代工具,这实际上是极不可能的。作为一名程序员,你编写出比成千上万人编写和测试过的工具更优化的代码(投入尽可能少的精力和时间)的机会非常渺茫。请注意,我不想用这句话侮辱您的编码技能。
    【解决方案4】:

    使用Cython 非常好。您可以使用类似 Python 的语法编写 C 扩展并让它生成 C 代码。包括样板。由于您已经在 python 中编写了代码,因此您只需对瓶颈代码进行一些更改,即可从中生成 C 代码。

    示例。 hello.pyx:

    cdef int hello(int a, int b):
        return a + b
    

    生成 601 行 样板代码:

    /* Generated by Cython 0.10.3 on Mon Jan 19 08:24:44 2009 */
    
    #define PY_SSIZE_T_CLEAN
    #include "Python.h"
    #include "structmember.h"
    #ifndef PY_LONG_LONG
      #define PY_LONG_LONG LONG_LONG
    #endif
    #ifndef DL_EXPORT
      #define DL_EXPORT(t) t
    #endif
    #if PY_VERSION_HEX < 0x02040000
      #define METH_COEXIST 0
    #endif
    #if PY_VERSION_HEX < 0x02050000
      typedef int Py_ssize_t;
      #define PY_SSIZE_T_MAX INT_MAX
      #define PY_SSIZE_T_MIN INT_MIN
      #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
      #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
      #define PyNumber_Index(o)    PyNumber_Int(o)
      #define PyIndex_Check(o)     PyNumber_Check(o)
    #endif
    #if PY_VERSION_HEX < 0x02060000
      #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
      #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)
      #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)
      #define PyVarObject_HEAD_INIT(type, size) \
              PyObject_HEAD_INIT(type) size,
      #define PyType_Modified(t)
    
      typedef struct {
           void *buf;
           PyObject *obj;
           Py_ssize_t len;
           Py_ssize_t itemsize;
           int readonly;
           int ndim;
           char *format;
           Py_ssize_t *shape;
           Py_ssize_t *strides;
           Py_ssize_t *suboffsets;
           void *internal;
      } Py_buffer;
    
      #define PyBUF_SIMPLE 0
      #define PyBUF_WRITABLE 0x0001
      #define PyBUF_LOCK 0x0002
      #define PyBUF_FORMAT 0x0004
      #define PyBUF_ND 0x0008
      #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
      #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
      #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
      #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
      #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
    
    #endif
    #if PY_MAJOR_VERSION < 3
      #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
    #else
      #define __Pyx_BUILTIN_MODULE_NAME "builtins"
    #endif
    #if PY_MAJOR_VERSION >= 3
      #define Py_TPFLAGS_CHECKTYPES 0
      #define Py_TPFLAGS_HAVE_INDEX 0
    #endif
    #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
      #define Py_TPFLAGS_HAVE_NEWBUFFER 0
    #endif
    #if PY_MAJOR_VERSION >= 3
      #define PyBaseString_Type            PyUnicode_Type
      #define PyString_Type                PyBytes_Type
      #define PyInt_Type                   PyLong_Type
      #define PyInt_Check(op)              PyLong_Check(op)
      #define PyInt_CheckExact(op)         PyLong_CheckExact(op)
      #define PyInt_FromString             PyLong_FromString
      #define PyInt_FromUnicode            PyLong_FromUnicode
      #define PyInt_FromLong               PyLong_FromLong
      #define PyInt_FromSize_t             PyLong_FromSize_t
      #define PyInt_FromSsize_t            PyLong_FromSsize_t
      #define PyInt_AsLong                 PyLong_AsLong
      #define PyInt_AS_LONG                PyLong_AS_LONG
      #define PyInt_AsSsize_t              PyLong_AsSsize_t
      #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask
      #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
      #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)
    #else
      #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)
      #define PyBytes_Type                 PyString_Type
    #endif
    #if PY_MAJOR_VERSION >= 3
      #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func)
    #endif
    #if !defined(WIN32) && !defined(MS_WINDOWS)
      #ifndef __stdcall
        #define __stdcall
      #endif
      #ifndef __cdecl
        #define __cdecl
      #endif
    #else
      #define _USE_MATH_DEFINES
    #endif
    #ifdef __cplusplus
    #define __PYX_EXTERN_C extern "C"
    #else
    #define __PYX_EXTERN_C extern
    #endif
    #include <math.h>
    #define __PYX_HAVE_API__helloworld
    
    #ifdef __GNUC__
    #define INLINE __inline__
    #elif _WIN32
    #define INLINE __inline
    #else
    #define INLINE 
    #endif
    
    typedef struct 
        {PyObject **p; char *s; long n; 
         char is_unicode; char intern; char is_identifier;} 
         __Pyx_StringTabEntry; /*proto*/
    
    static int __pyx_skip_dispatch = 0;
    
    
    /* Type Conversion Predeclarations */
    
    #if PY_MAJOR_VERSION < 3
    #define __Pyx_PyBytes_FromString PyString_FromString
    #define __Pyx_PyBytes_AsString   PyString_AsString
    #else
    #define __Pyx_PyBytes_FromString PyBytes_FromString
    #define __Pyx_PyBytes_AsString   PyBytes_AsString
    #endif
    
    #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
    static INLINE int __Pyx_PyObject_IsTrue(PyObject* x);
    static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x);
    static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x);
    static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b);
    
    #define __pyx_PyInt_AsLong(x) (PyInt_CheckExact(x) ? PyInt_AS_LONG(x) : PyInt_AsLong(x))
    #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
    
    static INLINE unsigned char __pyx_PyInt_unsigned_char(PyObject* x);
    static INLINE unsigned short __pyx_PyInt_unsigned_short(PyObject* x);
    static INLINE char __pyx_PyInt_char(PyObject* x);
    static INLINE short __pyx_PyInt_short(PyObject* x);
    static INLINE int __pyx_PyInt_int(PyObject* x);
    static INLINE long __pyx_PyInt_long(PyObject* x);
    static INLINE signed char __pyx_PyInt_signed_char(PyObject* x);
    static INLINE signed short __pyx_PyInt_signed_short(PyObject* x);
    static INLINE signed int __pyx_PyInt_signed_int(PyObject* x);
    static INLINE signed long __pyx_PyInt_signed_long(PyObject* x);
    static INLINE long double __pyx_PyInt_long_double(PyObject* x);
    #ifdef __GNUC__
    /* Test for GCC > 2.95 */
    #if __GNUC__ > 2 ||               (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 
    #define likely(x)   __builtin_expect(!!(x), 1)
    #define unlikely(x) __builtin_expect(!!(x), 0)
    #else /* __GNUC__ > 2 ... */
    #define likely(x)   (x)
    #define unlikely(x) (x)
    #endif /* __GNUC__ > 2 ... */
    #else /* __GNUC__ */
    #define likely(x)   (x)
    #define unlikely(x) (x)
    #endif /* __GNUC__ */
    
    static PyObject *__pyx_m;
    static PyObject *__pyx_b;
    static PyObject *__pyx_empty_tuple;
    static int __pyx_lineno;
    static int __pyx_clineno = 0;
    static const char * __pyx_cfilenm= __FILE__;
    static const char *__pyx_filename;
    static const char **__pyx_f;
    
    static void __Pyx_AddTraceback(const char *funcname); /*proto*/
    
    /* Type declarations */
    /* Module declarations from helloworld */
    
    static int __pyx_f_10helloworld_hello(int, int); /*proto*/
    
    
    /* Implementation of helloworld */
    
    /* "/home/nosklo/devel/ctest/hello.pyx":1
     * cdef int hello(int a, int b):             # <<<<<<<<<<<<<<
     *     return a + b
     * 
     */
    
    static  int __pyx_f_10helloworld_hello(int __pyx_v_a, int __pyx_v_b) {
      int __pyx_r;
    
      /* "/home/nosklo/devel/ctest/hello.pyx":2
     * cdef int hello(int a, int b):
     *     return a + b             # <<<<<<<<<<<<<<
     * 
     */
      __pyx_r = (__pyx_v_a + __pyx_v_b);
      goto __pyx_L0;
    
      __pyx_r = 0;
      __pyx_L0:;
      return __pyx_r;
    }
    
    static struct PyMethodDef __pyx_methods[] = {
      {0, 0, 0, 0}
    };
    
    static void __pyx_init_filenames(void); /*proto*/
    
    #if PY_MAJOR_VERSION >= 3
    static struct PyModuleDef __pyx_moduledef = {
        PyModuleDef_HEAD_INIT,
        "helloworld",
        0, /* m_doc */
        -1, /* m_size */
        __pyx_methods /* m_methods */,
        NULL, /* m_reload */
        NULL, /* m_traverse */
        NULL, /* m_clear */
        NULL /* m_free */
    };
    #endif
    static int __Pyx_InitCachedBuiltins(void) {
      return 0;
      return -1;
    }
    
    static int __Pyx_InitGlobals(void) {
      return 0;
      return -1;
    }
    
    #if PY_MAJOR_VERSION < 3
    PyMODINIT_FUNC inithelloworld(void); /*proto*/
    PyMODINIT_FUNC inithelloworld(void)
    #else
    PyMODINIT_FUNC PyInit_helloworld(void); /*proto*/
    PyMODINIT_FUNC PyInit_helloworld(void)
    #endif
    {
      __pyx_empty_tuple = PyTuple_New(0); 
      if (unlikely(!__pyx_empty_tuple))
          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; 
           __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      /*--- Library function declarations ---*/
      __pyx_init_filenames();
      /*--- Initialize various global constants etc. ---*/
      if (unlikely(__Pyx_InitGlobals() < 0)) 
         {__pyx_filename = __pyx_f[0]; 
          __pyx_lineno = 1; 
          __pyx_clineno = __LINE__; 
          goto __pyx_L1_error;}
      /*--- Module creation code ---*/
      #if PY_MAJOR_VERSION < 3
      __pyx_m = Py_InitModule4("helloworld", __pyx_methods, 0, 0, PYTHON_API_VERSION);
      #else
      __pyx_m = PyModule_Create(&__pyx_moduledef);
      #endif
      if (!__pyx_m) 
         {__pyx_filename = __pyx_f[0]; 
          __pyx_lineno = 1; __pyx_clineno = __LINE__; 
          goto __pyx_L1_error;};
      #if PY_MAJOR_VERSION < 3
      Py_INCREF(__pyx_m);
      #endif
      __pyx_b = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME);
      if (!__pyx_b) 
         {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; 
          __pyx_clineno = __LINE__; goto __pyx_L1_error;};
      if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) 
          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; 
           __pyx_clineno = __LINE__; goto __pyx_L1_error;};
      /*--- Builtin init code ---*/
      if (unlikely(__Pyx_InitCachedBuiltins() < 0)) 
          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; 
           __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_skip_dispatch = 0;
      /*--- Global init code ---*/
      /*--- Function export code ---*/
      /*--- Type init code ---*/
      /*--- Type import code ---*/
      /*--- Function import code ---*/
      /*--- Execution code ---*/
    
      /* "/home/nosklo/devel/ctest/hello.pyx":1
     * cdef int hello(int a, int b):             # <<<<<<<<<<<<<<
     *     return a + b
     * 
     */
      #if PY_MAJOR_VERSION < 3
      return;
      #else
      return __pyx_m;
      #endif
      __pyx_L1_error:;
      __Pyx_AddTraceback("helloworld");
      #if PY_MAJOR_VERSION >= 3
      return NULL;
      #endif
    }
    
    static const char *__pyx_filenames[] = {
      "hello.pyx",
    };
    
    /* Runtime support code */
    
    static void __pyx_init_filenames(void) {
      __pyx_f = __pyx_filenames;
    }
    
    #include "compile.h"
    #include "frameobject.h"
    #include "traceback.h"
    
    static void __Pyx_AddTraceback(const char *funcname) {
        PyObject *py_srcfile = 0;
        PyObject *py_funcname = 0;
        PyObject *py_globals = 0;
        PyObject *empty_string = 0;
        PyCodeObject *py_code = 0;
        PyFrameObject *py_frame = 0;
    
        #if PY_MAJOR_VERSION < 3
        py_srcfile = PyString_FromString(__pyx_filename);
        #else
        py_srcfile = PyUnicode_FromString(__pyx_filename);
        #endif
        if (!py_srcfile) goto bad;
        if (__pyx_clineno) {
            #if PY_MAJOR_VERSION < 3
            py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, 
                 __pyx_cfilenm, __pyx_clineno);
            #else
            py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, 
                 __pyx_cfilenm, __pyx_clineno);
            #endif
        }
        else {
            #if PY_MAJOR_VERSION < 3
            py_funcname = PyString_FromString(funcname);
            #else
            py_funcname = PyUnicode_FromString(funcname);
            #endif
        }
        if (!py_funcname) goto bad;
        py_globals = PyModule_GetDict(__pyx_m);
        if (!py_globals) goto bad;
        #if PY_MAJOR_VERSION < 3
        empty_string = PyString_FromStringAndSize("", 0);
        #else
        empty_string = PyBytes_FromStringAndSize("", 0);
        #endif
        if (!empty_string) goto bad;
        py_code = PyCode_New(
            0,            /*int argcount,*/
            #if PY_MAJOR_VERSION >= 3
            0,            /*int kwonlyargcount,*/
            #endif
            0,            /*int nlocals,*/
            0,            /*int stacksize,*/
            0,            /*int flags,*/
            empty_string, /*PyObject *code,*/
            __pyx_empty_tuple,  /*PyObject *consts,*/
            __pyx_empty_tuple,  /*PyObject *names,*/
            __pyx_empty_tuple,  /*PyObject *varnames,*/
            __pyx_empty_tuple,  /*PyObject *freevars,*/
            __pyx_empty_tuple,  /*PyObject *cellvars,*/
            py_srcfile,   /*PyObject *filename,*/
            py_funcname,  /*PyObject *name,*/
            __pyx_lineno,   /*int firstlineno,*/
            empty_string  /*PyObject *lnotab*/
        );
        if (!py_code) goto bad;
        py_frame = PyFrame_New(
            PyThreadState_GET(), /*PyThreadState *tstate,*/
            py_code,             /*PyCodeObject *code,*/
            py_globals,          /*PyObject *globals,*/
            0                    /*PyObject *locals*/
        );
        if (!py_frame) goto bad;
        py_frame->f_lineno = __pyx_lineno;
        PyTraceBack_Here(py_frame);
    bad:
        Py_XDECREF(py_srcfile);
        Py_XDECREF(py_funcname);
        Py_XDECREF(empty_string);
        Py_XDECREF(py_code);
        Py_XDECREF(py_frame);
    }
    
    /* Type Conversion Functions */
    
    static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b) {
      Py_ssize_t ival;
      PyObject* x = PyNumber_Index(b);
      if (!x) return -1;
      ival = PyInt_AsSsize_t(x);
      Py_DECREF(x);
      return ival;
    }
    
    static INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
       if (x == Py_True) return 1;
       else if (x == Py_False) return 0;
       else return PyObject_IsTrue(x);
    }
    
    static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x) {
        if (PyInt_CheckExact(x)) {
            return PyInt_AS_LONG(x);
        }
        else if (PyLong_CheckExact(x)) {
            return PyLong_AsLongLong(x);
        }
        else {
            PY_LONG_LONG val;
            PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
            val = __pyx_PyInt_AsLongLong(tmp);
            Py_DECREF(tmp);
            return val;
        }
    }
    
    static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
        if (PyInt_CheckExact(x)) {
            long val = PyInt_AS_LONG(x);
            if (unlikely(val < 0)) {
                PyErr_SetString(PyExc_TypeError, "Negative assignment to unsigned type.");
                return (unsigned PY_LONG_LONG)-1;
            }
            return val;
        }
        else if (PyLong_CheckExact(x)) {
            return PyLong_AsUnsignedLongLong(x);
        }
        else {
            PY_LONG_LONG val;
            PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
            val = __pyx_PyInt_AsUnsignedLongLong(tmp);
            Py_DECREF(tmp);
            return val;
        }
    }
    
    
    static INLINE unsigned char __pyx_PyInt_unsigned_char(PyObject* x) {
        if (sizeof(unsigned char) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            unsigned char val = (unsigned char)long_val;
            if (unlikely((val != long_val)  || (long_val < 0))) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to unsigned char");
                return (unsigned char)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE unsigned short __pyx_PyInt_unsigned_short(PyObject* x) {
        if (sizeof(unsigned short) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            unsigned short val = (unsigned short)long_val;
            if (unlikely((val != long_val)  || (long_val < 0))) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to unsigned short");
                return (unsigned short)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE char __pyx_PyInt_char(PyObject* x) {
        if (sizeof(char) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            char val = (char)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to char");
                return (char)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE short __pyx_PyInt_short(PyObject* x) {
        if (sizeof(short) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            short val = (short)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to short");
                return (short)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE int __pyx_PyInt_int(PyObject* x) {
        if (sizeof(int) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            int val = (int)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to int");
                return (int)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE long __pyx_PyInt_long(PyObject* x) {
        if (sizeof(long) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            long val = (long)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to long");
                return (long)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE signed char __pyx_PyInt_signed_char(PyObject* x) {
        if (sizeof(signed char) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            signed char val = (signed char)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed char");
                return (signed char)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE signed short __pyx_PyInt_signed_short(PyObject* x) {
        if (sizeof(signed short) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            signed short val = (signed short)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed short");
                return (signed short)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE signed int __pyx_PyInt_signed_int(PyObject* x) {
        if (sizeof(signed int) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            signed int val = (signed int)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed int");
                return (signed int)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE signed long __pyx_PyInt_signed_long(PyObject* x) {
        if (sizeof(signed long) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            signed long val = (signed long)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to signed long");
                return (signed long)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    
    static INLINE long double __pyx_PyInt_long_double(PyObject* x) {
        if (sizeof(long double) < sizeof(long)) {
            long long_val = __pyx_PyInt_AsLong(x);
            long double val = (long double)long_val;
            if (unlikely((val != long_val) )) {
                PyErr_SetString(PyExc_OverflowError, "value too large to convert to long double");
                return (long double)-1;
            }
            return val;
        }
        else {
            return __pyx_PyInt_AsLong(x);
        }
    }
    

    【讨论】:

    • 请注意,您也可以单独编写 C 代码,然后使用cython 将其映射到 Python 中。至少这是我使用它的方式(根据文档,这是一种完全合法的使用方式)。
    【解决方案5】:

    观察:基于 pybindgen 开发人员进行的基准测试,boost.python 和 swig 之间没有显着差异。我还没有进行自己的基准测试来验证这有多少取决于正确使用 boost.python 功能。

    还要注意,pybindgen 似乎通常比 swig 和 boost.python 快很多是有原因的:它可能不像其他两个那样产生通用的绑定。例如,异常传播、调用参数类型检查等。我还没有机会使用 pybindgen,但我打算这样做。

    Boost 通常是一个相当大的安装包,最后我看到你不能只安装 boost python 你几乎需要整个 Boost 库。正如其他人所提到的,由于大量使用模板编程,编译会很慢,这也意味着在编译时通常会出现相当神秘的错误消息。

    总结:鉴于 SWIG 的安装和使用非常容易,它生成了强大且通用的良好绑定,并且一个接口文件允许您的 C++ DLL 可以从其他几种语言(如 LUA、C# 和 Java)中使用,我会喜欢它而不是 boost.python。但除非你真的需要多语言支持,否则我会仔细研究 PyBindGen,因为它声称的速度,并密切关注它生成的绑定的稳健性和多功能性。

    【讨论】:

      【解决方案6】:

      这里有龙。不要痛饮,不要助长。对于任何复杂的项目,您必须自己填写以使其工作的代码很快就会变得难以管理。如果它是您的库的普通 C API(没有类),您可以只使用 ctypes。这将是轻松无痛的,您不必花费数小时浏览这些迷宫般的包装项目的文档,试图找到有关您需要的功能的一个小注释。

      【讨论】:

      • 存在这些其他包装器是因为 ctypes 不是每个人都想要的,你知道...
      【解决方案7】:

      由于您关心速度和开销,我建议考虑PyBindGen

      我有使用它来包装大型内部 C++ 库的经验。在尝试了 SWIG、SIP 和 Boost.Python 之后,我更喜欢 PyBindGen,原因如下:

      1. PyBindGen 包装器是纯 Python,无需学习其他文件格式
      2. PyBindGen 直接生成 Python C API 调用,没有像 SWIG 那样的抢速间接层。
      3. 生成的 C 代码简洁易懂。我也喜欢 Cython,但有时尝试读取其 C 输出可能会很困难。
      4. 支持 STL 序列容器(我们使用了很多 std::vector)

      【讨论】:

        【解决方案8】:

        如果它不是一个大的扩展,boost::python 也可能是一个选项,它比 swig 执行得更快,因为你可以控制正在发生的事情,但它会花费更长的时间来开发。

        如果单个调用中的工作量足够大,那么无论如何 swig 的开销是可以接受的。例如,如果您的问题是您有一些中等大小的逻辑块要移动到 C/C++,但该块在紧密循环中被调用,通常,您可能不得不避免痛饮,但我真的不认为除脚本图形着色器之外的任何真实示例。

        【讨论】:

          【解决方案9】:

          在放弃您的 Python 代码之前,请查看 ShedSkin。他们声称在某些代码上的性能比 Psyco 更好(并且还表示它仍处于试验阶段)。

          另外,将 C/C++ 代码绑定到 python 有多种选择。

          Boost 编译起来很长,但确实是最灵活且易于使用的解决方案。

          我从未使用过 SWIG,但与 boost 相比,它不如通用绑定框架灵活,而不是专用于 python 的框架。

          下一个选择是Pyrex。它允许编写被编译为 C 扩展的伪 Python 代码。

          【讨论】:

          • 我提到了boost,它是纯C++,所以你的评论是不恰当的。
          • 我指的是 PyRex。 Boost 可能是 swig 的一种更灵活的替代方案。但是我想知道在手写界面时是否有显着的性能提升,而不是让 swig 或 boost 创建代码。还是谢谢你的回答。 ShedSkin 对我来说是新的。
          • 很高兴这个答案提到了 ShedSkin,我认为这值得更多关注。另一方面,它提到了 Pyrex,它被 OP 已经提到的 Cython 完全取代。
          【解决方案10】:

          话题Cython, pybind11, cffi – which tool should you choose?有一篇文章值得一读

          急躁的人快速回顾一下:

          • Cython 将您的 python 编译为 C/C++,允许您将 C/C++ 嵌入到 python 代码中。使用静态绑定。对于python程序员。

          • pybind11(和 boost.python)正好相反。在编译时从 C++ 端绑定你的东西。对于 C++ 程序员。

          • CFFI 允许您在运行时动态绑定本机内容。使用简单,但性能损失更高。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-27
            • 2015-11-24
            • 1970-01-01
            • 1970-01-01
            • 2012-07-26
            • 1970-01-01
            相关资源
            最近更新 更多