Python中怎么调用C模块
导读:本文共3119字符,通常情况下阅读需要10分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 一.c,ctypes和python的数据类型的对应关系ctypes type ctype Python typec_char char 1-character stringc_wchar wchar_t 1-character unicode stringc_byte char int/longc_ubyte unsigned char int/longc_sh... ...
目录
(为您整理了一些要点),点击可以直达。一.c,ctypes和python的数据类型的对应关系
ctypes type ctype Python type
c_char char 1-character string
c_wchar wchar_t 1-character unicode string
c_byte char int/long
c_ubyte unsigned char int/long
c_short short int/long
c_ushort unsigned short int/long
c_int int int/long
c_uint unsigned int int/long
c_long long int/long
c_ulong unsigned long int/long
c_longlong __int64 or long long int/long
c_ulonglong unsigned __int64 or unsigned long long int/long
c_float float float
c_double double float
c_char_p char * (NUL terminated) string or None
c_wchar_p wchar_t * (NUL terminated) unicode or None
c_void_p void * int/long or None
2.操作int
>>>fromctypesimport*>>>c=c_int(34)>>>cc_int(34)>>>c.value34>>>c.value=343>>>c.value343
3.操作字符串
>>>p=create_string_buffer(10)>>>p.raw'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>>>p.value='fefefe'>>>p.raw'fefefe\x00\x00\x00\x00'>>>p.value='fefeeeeeeeeeeeeeeeeeeeeeee'#字符串太长,报错Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:stringtoolong
4.操作指针
>>>i=c_int(999)>>>pi=pointer(i)>>>pi<__main__.LP_c_intobjectat0x7f7be1983b00>>>>pi.valueTraceback(mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'LP_c_int'objecthasnoattribute'value'>>>pi.contentsc_int(999)>>>pi.contents=c_long(34343)>>>pi.contentsc_int(34343)
通过pointer获取一个值的指针
通过contents获取一个指针的值
5.c的结构体
#定义一个c的structure,包含两个成员变量x和y>>>classPOINT(Structure):..._fields_=[('x',c_int),('y',c_int)]...>>>point=POINT(2,4)>>>point<__main__.POINTobjectat0x7f7be1983b90>>>>point.x,point.y(2,4)>>>porint=POINT(y=2)>>>porint<__main__.POINTobjectat0x7f7be1983cb0>>>>point=POINT(y=2)>>>point.x,point.y(0,2)定义一个类型为POINT的数组>>>POINT_ARRAY=POINT*3>>>pa=POINT_ARRAY(POINT(2,3),POINT(2,4),POINT(2,5))>>>foriinpa:printpa.y...Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>AttributeError:'POINT_Array_3'objecthasnoattribute'y'>>>foriinpa:printi.y...345
6.访问so文件
1.创建一个c文件
#include<stdio.h>inthello_world(){printf("HelloWorld\n");return0;}intmain(){hello_world();return0;}
2.编译成动态链接库
gcchello_world.c-fPIC-shared-ohello_world.so
3.python中调用库中的函数
fromctypesimportcdllc_lib=cdll.LoadLibrary('./hello_world.so')c_lib.hello_world()
二.测试c的性能和python的差别
sum.c
intsum(intnum){longsum=0;inti=0;for(i=1;i<=num;i++){sum=sum+i;};returnsum;}intmain(){printf("%d",sum(10));return0;}
测试方案:计算1-100的和
测试次数:100万次
1. 直接用c来执行,通linux 的time命令来记录执行的用时
sum.c:
#include<stdio.h>intsum(intnum){longsum=0;inti=0;for(i=1;i<=num;i++){sum=sum+i;};returnsum;}intmain(){inti;for(i=0;i<1000000;i++){sum(100);}return0;}
测试结果的例子:
real 1.16
user 1.13
sys 0.01
2.通过Python调用so文件和python的测试结果
sum_test.py:
defsum_python(num):s=0foriinxrange(1,num+1):s+=ireturnsfromctypesimportcdllc_lib=cdll.LoadLibrary('./sum.so')defsum_c(num):returnc_lib.sum(num)deftest(num):importtimeitt1=timeit.Timer('c_lib.sum(%d)'%num,'from__main__importc_lib')t2=timeit.Timer('sum_python(%d)'%num,'from__main__importsum_python')print'c',t1.timeit(number=1000000)print'python',t2.timeit(number=1000000)if__name__=='__main__':test(100)
测试结果的例子
c1.02756714821python7.90672802925
3.测试erlang的测试结果
刚刚学了erlang,那就一起测试一下erlang的运算性能
sum.erl:
-module(sum).-export([sum/2,sum_test/2]).sum(0,Sum)->Sum;sum(Num,Sum)->sum(Num-1,Sum+Num).sum_test(Num,0)->0;sum_test(Num,Times)->sum(Num,0),sum_test(Num,Times-1).
调用:
timer:tc(sum,sum_test,[100,1000000]).
测试结果的例子:
{2418486,0}
4.测试结果
用上面的测试方法,进行10次测试,去除***值和最小值,再计算平均值,得出:
单位:秒
求和的运行,使用的内存比较小,但是占用CPU资源比较多。
原生的C是最快的,Python调用c会稍微慢一点,原因是计算100的和的操作是在c里面做的,而执行100万次的逻辑是在python做的
erlang的性能虽然比c稍慢,但是也是不错的,
Python的运行效率惨不忍睹。。。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Python中怎么调用C模块的详细内容,希望对您有所帮助,信息来源于网络。