如何在C++中有效地构建Python字典

tob*_*gue 4 c++ python performance dictionary python-extensions

出于性能原因,我想将部分python程序移植到C++中,因此我尝试为程序编写一个简单的扩展.C++部分将构建一个字典,然后需要将其传递给Python程序.

我发现的一种方法似乎是在C++中构建我的类似dict的对象,例如a boost::unordered_map,然后使用Py_BuildValue[1]方法将其转换为Python ,该方法能够生成Python dicts.但是这种方法包括将容器转换为字符串表示并返回看起来有点太多"即将到来"才能成为最高性能的解决方案!?

所以我的问题是:在C++中构建Python字典的最高效方法什么?我看到boost有一个Python库,它支持在C++和Python之间映射容器,但到目前为止我没有在文档中找到我需要的东西.如果有这样的方式我宁愿在C++中直接构建Python dict,那么就不需要复制等.但如果最有效的方法是另一种方式,我也很擅长.

这是(简化的)C++ - 我编译成.dll/.pyd的代码:

#include <iostream>
#include <string>
#include <Python.h>
#include "boost/unordered_map.hpp"
#include "boost/foreach.hpp"

extern "C"{
typedef boost::unordered_map<std::string, int> hashmap;

static PyObject*
_rint(PyObject* self, PyObject* args)
{
    hashmap my_hashmap; // DO I NEED THIS?
    my_hashmap["a"] = 1; // CAN I RATHER INSERT TO PYTHON DICT DIRECTLY??
    BOOST_FOREACH(hashmap::value_type i, my_hashmap) {
            // INSERT ELEMENT TO PYTHON DICT
    }
    // return PYTHON DICT
}

static PyMethodDef TestMethods[] = {
    {"rint", _rint, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
inittest(void)
{
    Py_InitModule("test", TestMethods);
}

} // extern "C"
Run Code Online (Sandbox Code Playgroud)

我想在Python中使用:

import test
new_dict = test.rint()
Run Code Online (Sandbox Code Playgroud)

字典将字符串映射到整数.谢谢你的帮助!

tit*_*ito 5

  • 直接使用CPython API是:
    PyObject *d = PyDict_New()
    for (...) {
      PyDict_SetItem(d, key, val);
    }
    return d;
  • 或者通过覆盖__setitem__和编写一个模拟dict的python对象__getitem__.在这两种方法中,使用原始的hashmap.最后,不会发生任何副本!