简体   繁体   English

将 **float 数组从 C++ Dll 传递到 python

[英]Passing **float array from C++ Dll to python

I am currently working with C/C++ functions imported into python thanks to a Dll and ctypes.由于 Dll 和 ctypes,我目前正在使用导入到 python 的 C/C++ 函数。 In the DLL, I have a function that takes a float **variable as argument.在 DLL 中,我有一个 function 以浮点 ** 变量作为参数。 This argument is filled in the function and I get it back on python using ctypes.这个参数填充在 function 中,我使用 ctypes 在 python 上取回它。

I have a solution which works using 2 for loops, but this is very slow when the 2D array has a lot of points.我有一个使用 2 个 for 循环的解决方案,但是当 2D 数组有很多点时,这非常慢。

Do you know a way to do without these for loops?你知道没有这些 for 循环的方法吗?

Here is my code:这是我的代码:

Short version of the exported function (DLL)导出的 function (DLL) 的短版

unsigned short ClassExample::GetData(float **Samples)
{
    for(long i = 0; i < rows; i++)
    {
        try
        {
            if(ch < NB_ROWS_MAX) 
            {
                Samples[ch]][i] = this->Data[ch][i];
            }
            else
            {
                ...
            }
        } ...

Python code Python代码

def GetSamples(self, ROWS, COLUMN):

    Data = (ctypes.POINTER(ctypes.c_float) * ROWS)()
    for i in range(COLUMN):
        Data[i] = (ctypes.c_float * COLUMN)()

    try:
        succeed = lib._PyGetData(ClassExampleObj, ctypes.byref(Data))
        if succeed != 0:
            print("Error number :", succeed)
    except:
        print("Unknown Error")

    Data_np = numpy.zeros((ROWS, COLUMN), dtype=float)
    for i in range(ROWS):
        for j in range(COLUMN):
            Data_np[i, j] = float(Data[i][j])
    return Data_np

Thank you !谢谢 !

If your problem is efficiency then you probably are working with very very large datasets.如果您的问题是效率,那么您可能正在使用非常非常大的数据集。

The easiest way to make a 2-d matrix iteration much faster is to make it 1-d.使二维矩阵迭代更快的最简单方法是使其成为一维。 If you have X columns and Y rows, then instead of accessing matrix[I][j] you can access matrix[i*X+j] , which saves you a dereference, and possibly many cache-misses.如果您有 X 列和 Y 行,那么您可以访问matrix[i*X+j]而不是访问matrix[I][j] ] ,这样可以节省您的取消引用,并且可能会避免许多缓存未命中。 If possible, you could even go further and treat the matrix as a 1-d array.如果可能的话,您甚至可以进一步 go 并将矩阵视为一维数组。

Another time-saving solution is to avoid conditions as much as possible.另一个节省时间的解决方案是尽可能避免条件。 If you only have conditions inside the for-loop, the compiler will be able to greatly optimize it.如果你只在 for 循环内有条件,编译器将能够大大优化它。

Without seeing more of your code it is hard to tell whether you have an inefficient code or simply handling too much data for your computer, but the guidelines I gave seem to fit from the example you gave.在没有看到更多代码的情况下,很难判断您是否有效率低下的代码或只是为您的计算机处理了太多数据,但我给出的指导方针似乎与您给出的示例相符。

The last thing I want to mention is that float is quite imprecise and you better use double if possible.我要提到的最后一件事是float非常不精确,如果可能的话,你最好使用double

Good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM