如何处理C ++返回类型std :: vector< int>在Python ctypes? [英] How to handle C++ return type std::vector<int> in Python ctypes?

查看:1822
本文介绍了如何处理C ++返回类型std :: vector< int>在Python ctypes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到ctypes如何弥合 std :: vector 和Python之间的差距;没有在互联网上的地方是提到的组合。这是不好的做法,它不存在还是我错过了什么?



C ++ :xxx.cpp

  #include< fstream> 
#include< string>
using namespace std;
externCstd :: vector< int> foo(const char * FILE_NAME)
{
string line;
std :: vector< int>结果;
ifstream myfile(FILE_NAME);
while(getline(myfile,line)){
result.push_back(1);
}

return(result);
}

Python:

  import ctypes 
xxx = ctypes.CDLL(./ libxxx.so)
xxx.foo.argtypes = ?
xxx.foo.restype = ?????


解决方案


那个速度很重要。我创建一个应该能够处理大数据的
应用程序。在200,000行
上,错过数必须计入300个值(200k乘300矩阵)。 I
相信,但是如果我错了,请纠正我,C ++将显着
更快。


好吧,如果你从一个大文件读取,你的过程将是大多数IO绑定,所以Python和C之间的时间可能不会有很大的不同。



以下代码...

  result = [] 
'):
result.append(line.count('NA'))

。 ..seems运行一样快,任何我可以在C中一起攻击,虽然它使用一些优化算法我不是很熟悉。



处理200,000行需要不到一秒钟,感兴趣的是看看是否可以写出显着更快的C函数。








如果你想在C中做,最终得到一个Python列表,使用 Python / C API 自行构建列表,而不是构建一个 std :: vector

一个示例,它只返回一个从0到99的整数列表...

  // hack.c 

#include< python2.7 / Python.h>

PyObject * foo(const char * filename)
{
PyObject * result = PyList_New(0);
int i;

for(i = 0; i <100; ++ i)
{
PyList_Append(result,PyInt_FromLong(i));
}

返回结果;
}

编译...


$ b b

  $ gcc -c hack.c -fPIC 
$ ld -o hack.so -shared hack.o -lpython2.7

使用示例...

 >>>> from ctypes import * 
>>>> dll = CDLL('./ hack.so')
>>>> dll.foo.restype = py_object
>>>> dll.foo('foo')
[0,1,2,3,4,5,6,7,8,9,...]
pre>

I cannot find how ctypes will bridge the gap between std::vector and Python; no where on the internet is the combination mentioned. Is this bad practice, does it not exist or am I missing something?

C++ : xxx.cpp

#include <fstream>
#include <string>
using namespace std;
extern "C" std::vector<int> foo(const char* FILE_NAME)
{
    string line;
    std::vector<int> result;
    ifstream myfile(FILE_NAME);
    while (getline(myfile, line)) {
      result.push_back(1);
    }

    return(result);
}

Python: xxx.py

import ctypes
xxx = ctypes.CDLL("./libxxx.so")
xxx.foo.argtypes = ??????
xxx.foo.restype = ??????

解决方案

The particular reason is that speed is important. I'm creating an application that should be able to handle big data. On 200,000 rows the missings have to be counted on 300 values (200k by 300 matrix). I believe, but correct me if I'm wrong, that C++ will be significantly faster.

Well, if you're reading from a large file, your process is going to be mostly IO-bound, so the timings between Python and C probably won't be significantly different.

The following code...

result = []
for line in open('test.txt'):
    result.append(line.count('NA'))

...seems to run just as fast as anything I can hack together in C, although it's using some optimized algorithm I'm not really familiar with.

It takes less than a second to process 200,000 lines, although I'd be interested to see if you can write a C function which is significantly faster.


Update

If you want to do it in C, and end up with a Python list, it's probably more efficient to use the Python/C API to build the list yourself, rather than building a std::vector then converting to a Python list later on.

An example which just returns a list of integers from 0 to 99...

// hack.c

#include <python2.7/Python.h>

PyObject* foo(const char* filename)
{
    PyObject* result = PyList_New(0);
    int i;

    for (i = 0; i < 100; ++i)
    {
        PyList_Append(result, PyInt_FromLong(i));
    }

    return result;
}

Compiled with...

$ gcc -c hack.c -fPIC
$ ld -o hack.so -shared hack.o -lpython2.7

Example of usage...

>>> from ctypes import *
>>> dll = CDLL('./hack.so')
>>> dll.foo.restype = py_object
>>> dll.foo('foo')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]

这篇关于如何处理C ++返回类型std :: vector&lt; int&gt;在Python ctypes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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