Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ctypes提高函数调用的性能 我试图用Python代码调用C++编写的函数,我使用的方法是cType。下面是我如何做包装的: def render(vertex, tri, texture, src_img): cRenderer = ctypes.cdll.LoadLibrary("renderer.so") cvertex = ((ctypes.c_double * len(vertex))(*vertex)) nver = int(vertex.shape[0] / 3) ntri = tri.shape[0] tri = np.hstack(tri) ctri = ((ctypes.c_double * len(tri))(*tri)) texture = np.hstack(texture) ctexture = ((ctypes.c_double * len(texture))(*texture)) width = src_img.shape[0] height = src_img.shape[1] nChannels = src_img.shape[2] src_img = np.hstack(np.hstack(src_img)) csrc_img = ((ctypes.c_double * len(src_img))(*src_img)) cimg = ((ctypes.c_double * len(src_img))(*src_img)) cRenderer.render(cvertex, ctri, ctexture, nver, ntri, csrc_img, width, height, nChannels, cimg) img = np.array(cimg).reshape(nChannels, height*width) return img.reshape(width, height, nChannels) C++函数采用顶点、三角形、纹理、少量数字和输入/输出图像的数组。_Python_C++_Performance_Optimization_Ctypes - Fatal编程技术网

使用ctypes提高函数调用的性能 我试图用Python代码调用C++编写的函数,我使用的方法是cType。下面是我如何做包装的: def render(vertex, tri, texture, src_img): cRenderer = ctypes.cdll.LoadLibrary("renderer.so") cvertex = ((ctypes.c_double * len(vertex))(*vertex)) nver = int(vertex.shape[0] / 3) ntri = tri.shape[0] tri = np.hstack(tri) ctri = ((ctypes.c_double * len(tri))(*tri)) texture = np.hstack(texture) ctexture = ((ctypes.c_double * len(texture))(*texture)) width = src_img.shape[0] height = src_img.shape[1] nChannels = src_img.shape[2] src_img = np.hstack(np.hstack(src_img)) csrc_img = ((ctypes.c_double * len(src_img))(*src_img)) cimg = ((ctypes.c_double * len(src_img))(*src_img)) cRenderer.render(cvertex, ctri, ctexture, nver, ntri, csrc_img, width, height, nChannels, cimg) img = np.array(cimg).reshape(nChannels, height*width) return img.reshape(width, height, nChannels) C++函数采用顶点、三角形、纹理、少量数字和输入/输出图像的数组。

使用ctypes提高函数调用的性能 我试图用Python代码调用C++编写的函数,我使用的方法是cType。下面是我如何做包装的: def render(vertex, tri, texture, src_img): cRenderer = ctypes.cdll.LoadLibrary("renderer.so") cvertex = ((ctypes.c_double * len(vertex))(*vertex)) nver = int(vertex.shape[0] / 3) ntri = tri.shape[0] tri = np.hstack(tri) ctri = ((ctypes.c_double * len(tri))(*tri)) texture = np.hstack(texture) ctexture = ((ctypes.c_double * len(texture))(*texture)) width = src_img.shape[0] height = src_img.shape[1] nChannels = src_img.shape[2] src_img = np.hstack(np.hstack(src_img)) csrc_img = ((ctypes.c_double * len(src_img))(*src_img)) cimg = ((ctypes.c_double * len(src_img))(*src_img)) cRenderer.render(cvertex, ctri, ctexture, nver, ntri, csrc_img, width, height, nChannels, cimg) img = np.array(cimg).reshape(nChannels, height*width) return img.reshape(width, height, nChannels) C++函数采用顶点、三角形、纹理、少量数字和输入/输出图像的数组。,python,c++,performance,optimization,ctypes,Python,C++,Performance,Optimization,Ctypes,我以最原始的方式编写了代码,因为这是我第一次使用ctypes。 当我开始进行一些分析时,我发现函数大部分时间都花在转换数组上,例如: cvertex = ((ctypes.c_double * len(vertex))(*vertex)) 有没有一种方法可以更有效地将此转化为一个可以通过的论点? 除了ctypes还有其他方法可以提供更好的性能吗 任何其他加速此函数的建议都是受欢迎的:)我相信np.array.ctypes.data返回一个指针,指向可以传递给ctypes的原始数组数据。

我以最原始的方式编写了代码,因为这是我第一次使用ctypes。 当我开始进行一些分析时,我发现函数大部分时间都花在转换数组上,例如:

cvertex = ((ctypes.c_double * len(vertex))(*vertex))
有没有一种方法可以更有效地将此转化为一个可以通过的论点? 除了ctypes还有其他方法可以提供更好的性能吗


任何其他加速此函数的建议都是受欢迎的:)

我相信
np.array.ctypes.data
返回一个指针,指向可以传递给ctypes的原始数组数据。