日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中调用C++函数

發布時間:2025/3/15 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中调用C++函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python中調用C++函數

  • 無參調用
  • 單變量傳入與返回調用
  • numpy數組傳入與返回調用
  • c++類調用
  • 用python寫不香嗎?還這么復雜調用C++?

一、 無參調用

在python中調用無參數和無返回的C++函數,具體的使用方式如下:
C++API函數的編寫形式:

#include<iostream> #include<string> using namespace std;extern "C"{int hehe(void){cout << "hello world!" << endl;return 0;} }

將編寫的.cpp文件生成.so動態鏈接庫在ubuntu命令行中執行如下命令:

g++ -o xxx.os -shared -fPIC xxx.cpp

在相應的文件下就可看到生成的xxx.so文件。
python調用生成的動態鏈接庫:

import ctypesdll = ctypes.cdll.LoadLibrary('./doc.so') dll.hehe()

運行python文件得到如下結果:

二、單變量傳入與返回調用

在C++函數中需要python程序傳入一個單變量的參數,整形,浮點型或者字符型。其中對應關系如下:
C++的程序如下:

#include <iostream> #include <string>using namespace std;extern "C"{int hehe(int a){int i = 0;i = a * a;return i;} }

對用python的程序為:

import ctypesdll = ctypes.cdll.LoadLibrary('./doc.so') a = 23 a2 = dll.hehe(a) print(a2)

運行結果:

三、numpy數組傳入與返回調用

對于numpy數組傳入C++函數與之前的但變量傳入大不相同:
1、首先對numpy數組處理得到存儲位置的指針(切記numpy數組內存一定要連續)
2、將得到的指針傳遞給對應的C++函數。
在這個過程中,會有一個問題出現,比如numpy數組為一個二維的矩陣,但C++得到參數只是一個指針,所以我們要有一個計算。將多維空間中的位置坐標轉換為一維中的指針下標:舉例如下:
假設有一個多維的矩陣MxNxH,轉換為內存中的實際存儲順序為第一行第一列的H個元素-第一行第二列的H個元素。。。依次類推
3、對于傳入指針的返回,因為傳入的是實際物理地址,所以在C++函數中對數據的改變,在python對應的也會發生相應的改變。
具體C++程序為:

#include <iostream> #include <string>using namespace std;extern "C"{void hehe(int* a,int rows,int cols){int row,col;for(row = 0;row < rows; row++){for(col = 0;col < cols;col++){int ind = row * cols + col;a[ind] = a[ind]* a[ind];}}} }

與之相對應的python文件為:

import ctypes import numpy as npdll = ctypes.cdll.LoadLibrary('./doc.so') a = np.array([[1,2,3],[2,3,4],[3,4,5]],dtype=np.int32) p = a.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) print(a) dll.hehe(p,3,3) print(a)

運行python文件得到的結果為:

四、numpy數組傳入與返回調用

class test{publicvoid test(void); }extern "C"{test t;void test(void){t.test();} } void test::test(void) {cout << "hello world!" << endl; }

用python寫不香嗎?還這么復雜調用C++?

別的方面具體不不清楚兩者的區別,但是就從處理速度來說,我做了一個小實驗,相信看完之后就沒有類似的疑問了。實驗的內容為:對于一個512x512x10全一矩陣執行所有元素加1操作,分別用python和C++來執行,看需要的時間。
具體的代碼:

#include<iostream> #include<string>using namespace std;extern "C"{ void hehe(int* p ,int row,int col,int channel){int h,w,c;for(h = 0;h < row;h++){for(w = 0;w < col;w++){for(c = 0;c < channel;c++){int ind = h*(col * channel) + w * channel + c;p[ind] = p[ind] + 1; }}} }}#include<iostream> #include<string>using namespace std;extern "C"{ void hehe(int* p ,int row,int col,int channel){int h,w,c;for(h = 0;h < row;h++){for(w = 0;w < col;w++){for(c = 0;c < channel;c++){int ind = h*(col * channel) + w * channel + c;p[ind] = p[ind] + 1; }}} }} import ctypes as ct import numpy as np import timedef add1(m,h,w,c):for row in range(h):for col in range(w):for channel in range(c):m[row][col][channel] += 1 ll = ct.cdll.LoadLibrary("./test.so")m = np.ones([10,512,512],np.int32) print(m.shape) h,w,c = m.shape[:3] p = m.ctypes.data_as(ct.POINTER(ct.c_int32)) t1 = time.time() ll.hehe(p,h,w,c) print('c++ time:',time.time()-t1) t2 = time.time() add1(m,h,w,c) print('python time:',time.time()-t2)

運行結果(睜大眼睛看清楚喲):

總結

以上是生活随笔為你收集整理的python中调用C++函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。