python调用C++程序

python编程方便,C++运行效率高
在写python程序中如果需要提效的逻辑单元可以通过C++语言实现

python调用C程序.

一.C语言测试逻辑

step1: g++环境

step2: 代码文件tmp.c

/*
char    1 个字节    -128 到 127 或者 0 到 255
unsigned char    1 个字节    0 到 255
signed char    1 个字节    -128 到 127
int    4 个字节    -2147483648 到 2147483647
unsigned int    4 个字节    0 到 4294967295
signed int    4 个字节    -2147483648 到 2147483647
short int    2 个字节    -32768 到 32767
unsigned short int    2 个字节    0 到 65,535
signed short int    2 个字节    -32768 到 32767
long int    8 个字节    -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int    8 个字节    -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int    8 个字节    0 到 18,446,744,073,709,551,615
float    4 个字节    精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double    8 个字节    双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double    16 个字节    长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t    2 或 4 个字节    1 个宽字符

*/

#include 
using namespace std;

long return_c_num(long num){
    cout<<"c++ num="<<num<<endl;
    return num;
}


long add_c(long d){
    cout<<"d="<<d<<endl;
    long sum = 0;
    for (; d; d--){
        sum += d;
    }
    cout<<"sum="<<sum<<endl;
    return sum;
}

extern "C" {

	long add_c_plus(long d) {
		return add_c(d);
	}

	long return_c_plus_num(long num){
	    return return_c_num(num);
	}

}


step3:

g++ -Wall -O3 -shared tmp.cpp -o tmp.so

生成文件tmp.so

二.python调用测试

code: python_use_c_plus.py

#-*- coding:UTF-8 -*-
import ctypes


from ctypes import cdll
import time

tmp_c = cdll.LoadLibrary("./tmp.so")
print(tmp_c)
# print(tmp_c.return_c_num)

def add_circle_test_c(num):
    start_time = time.time()
    tmp_c.add_c_plus(num)
    end_time = time.time()
    print(f'cost {end_time-start_time} ms')


def add_circle_test_python3(num):
    print(f'd={num}')
    start_time = time.time()
    sum = 0
    for i in range(num+1):
        sum += i
    print(f'sum={sum}')
    end_time = time.time()
    print(f'cost {end_time-start_time} ms')


def get_c_num(num):
    return_result = tmp_c.return_c_plus_num(num)
    print(f"python num={return_result}")


if __name__ == '__main__':
    num = 10000000
    print('### test c++ ###')
    add_circle_test_c(num)
    print('### test python ###')
    add_circle_test_python3(num)

    print('\n')
    print('测试内存溢出')
    num =  2147483647
    print(f'origin num: {num}')
    get_c_num(num)
    num = 2147483648
    print(f'origin num: {num}')
    get_c_num(num)

运行结果分析:

### test c++ ###
d=10000000
sum=50000005000000
cost 3.0279159545898438e-05 ms
### test python ###
d=10000000
sum=50000005000000
cost 0.534045934677124 ms


测试内存溢出
origin num: 2147483647
c++ num=2147483647
python num=2147483647
origin num: 2147483648
c++ num=-2147483648
python num=-2147483648

时效

1000W次叠加,python耗时0.53s,C++耗时0.00003s,C++提效显著

内存溢出(注意点)

超过一个限度:
i n t 4 字 节 − 2 , 147 , 483 , 648 到 2 , 147 , 483 , 647 int 4 字节 -2,147,483,648 到 2,147,483,647 int42,147,483,6482,147,483,647
python只用4个字节存储,则python接受的数据会有问题

你可能感兴趣的:(python,python)