python调用C传参整形,字符串,结构体,数组

本文介绍Python通过LoadLibrary调用C程序的方法,演示了如何使用ctypes库调用动态链接库中的函数,包括基本类型、字符串、结构体、数组及排序函数的传递与调用,并对比了Python与C在执行效率上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python调用c程序像JNI样LoadLibrary(“xx.dll or xx.so"),也能使用boost工具,swig工具将c程序封装为Module,直接import使用

这里使用的为LoadLibraray方法调用动态链接库中函数

xx.dll :windows平台下的动态链接库

xx.so : linux平台下的动态链接库

目前认为JNI,python调用c,可以用c实现的数据结构和算法,当然是c程序的运行的快

 py_use_c.c

#include <stdio.h>
int sum(int a,int b){
	return a+b;
}
void str_p(char* str){
	printf("with char* transmit the str: %s \n",str);
}
void str_char(char str[]){
	printf("with char[] transmit the str: %s \n",str);
}
typedef struct entity
{
	int key;
	char* value;	
}Entity;
void typedef_structure_o(Entity entity){
	printf("The Entity's key=%d,value=%s \n",entity.key,entity.value);
}
void typedef_structure_p(Entity* pentity){
	printf("The Entity's key=%d,value=%s \n",pentity->key,pentity->value);
}
void array(int a[],int len){
	printf("you transmit the array:{");
	for(int i = 0; i < len; i++) {
		if(i==len-1){
			printf("%d",a[i]);
		}else{
			printf("%d,",a[i]);
		}
	}
	printf("}\n");
}
void bubble_sort(int a[],int len){
	int temp;
	for(int i = 0; i < len-1; i++){
		for(int j = 0; j < len-i-1; j++){
			if(a[j]>a[j+1]){
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
}

useCprogram.py

import ctypes 
from ctypes import *
c_so = ctypes.CDLL("./py_use_c.so")
#transmit int 
result = c_so.sum(1,2) 
print(result)
#transmit str to char*
c_so.str_p(bytes("lingo",encoding="utf-8"))
#transmit str to char[]
c_so.str_char(bytes("lingo",encoding="utf-8"))
#transmit typedef structure
#design class for typedef structure
# c_int is int
# c_char_p is char*
class Typedef_Structure(Structure):
	_fields_=[("key",c_int),("value",c_char_p)] 
myStructure = Typedef_Structure()
myStructure.key = 1
myStructure.value = bytes("lingo",encoding="utf-8")
c_so.typedef_structure_o(myStructure)
#transmit typedef structure *
c_so.typedef_structure_p(byref(myStructure))
#transmit Arrayx
arr = (c_int* 10)()
for i in range(0,len(arr)):
	arr[i] = i
c_so.array(arr,len(arr))

#1,前提,我们需要将c程序编译为动态链接库

windows :gcc -shared -fpic py_use_c.c -o py_use_c.dll

若你使用的python虚拟机为64位时你gcc编译的动态链接库为32位时报错:OSError: [WinError 193] %1 不是有效的 Win32 应用程序。

linux:gcc -shared -fpic py_use_c.c -o py_use_c.so

#2 这里在ubuntu环境下执行py程序 

知道如何调用c程序的函数后,我们可以用传参数组调用bubble_sort(冒泡排序)看看python和python_use_c谁更快,感受下python的慢和c效率,毕竟c是**(hhh)

#There test the bubble_sort for python and python_use_c
list = [3,4,5,11,66,44,6,3,4,56,86,88,23,55,66,2,3,55,66,77,8,8,99]
def bubble_sort(list):
	for i in range(len(list)):
		for j in range(len(list)-1-i):
			if(list[j] > list[j+1]):
				temp = list[j+1]
				list[j+1] = list[j]
				list[j] = temp
			j+=1
		i+=1
import time
before = time.time()
for i in range(10000):
	bubble_sort(list)
print(time.time()-before)
c_arr = (c_int* len(list))()
# list --> c_array
for i in range(0,len(c_arr)):
	c_arr[i] = list[i]
before = time.time()
for i in range(10000):
	c_so.bubble_sort(arr,len(c_arr))
print(time.time()-before)

执行结果:

 

大三上学期,学校开了python课,学着感兴趣的方面,毕竟若是考上研究生后我觉得会多多用到,python的框架繁多(奇淫巧技,hh)不得不承认python的强项方面,以前认为把一个Java(嵌入式 开发或企业级Web开发,ssm or ssh 赚钱,学习hadoop,大数据开发,——学好就可以提前离校实习,但久而久之认识到一个985,211的研究生学位,研究生的经历和对自身的影响会很大,有些时候要以睁大眼睛看清自己的未来。在准备考试的阶段不断提升c/c++,多多搞搞数据结构和算法(这是是内功啊)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值