Python调用C模块以及性能分析

开发 后端
本文讲述Python调用C模块以及性能分析的内容。

Python调用C模块以及性能分析

一.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

  1. >>> from ctypes import * 
  2.  
  3. >>> c=c_int(34) 
  4.  
  5. >>> c 
  6.  
  7. c_int(34) 
  8.  
  9. >>> c.value 
  10.  
  11. 34 
  12.  
  13. >>> c.value=343 
  14.  
  15. >>> c.value 
  16.  
  17. 343  

3.操作字符串

  1. >>> p=create_string_buffer(10) 
  2.  
  3. >>> p.raw 
  4.  
  5. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 
  6.  
  7. >>> p.value='fefefe' 
  8.  
  9. >>> p.raw 
  10.  
  11. 'fefefe\x00\x00\x00\x00' 
  12.  
  13. >>> p.value='fefeeeeeeeeeeeeeeeeeeeeeee'  #字符串太长,报错 
  14.  
  15. Traceback (most recent call last): 
  16.  
  17.   File "<stdin>", line 1, in <module> 
  18.  
  19. ValueError: string too long  

4.操作指针

  1. >>> i=c_int(999) 
  2.  
  3. >>> pi=pointer(i) 
  4.  
  5. >>> pi 
  6.  
  7. <__main__.LP_c_int object at 0x7f7be1983b00> 
  8.  
  9. >>> pi.value 
  10.  
  11. Traceback (most recent call last): 
  12.  
  13.   File "<stdin>", line 1, in <module> 
  14.  
  15. AttributeError: 'LP_c_int' object has no attribute 'value' 
  16.  
  17. >>> pi.contents 
  18.  
  19. c_int(999) 
  20.  
  21. >>> pi.contents=c_long(34343) 
  22.  
  23. >>> pi.contents 
  24.  
  25. c_int(34343)  
  • 通过pointer获取一个值的指针
  • 通过contents获取一个指针的值

5.c的结构体

  1. #定义一个c的structure,包含两个成员变量x和y 
  2.  
  3. >>> class POINT(Structure): 
  4.  
  5. ...     _fields_=[('x',c_int),('y',c_int)] 
  6.  
  7. ... 
  8.  
  9. >>> point=POINT(2,4) 
  10.  
  11. >>> point 
  12.  
  13. <__main__.POINT object at 0x7f7be1983b90> 
  14.  
  15. >>> point.x,point.y 
  16.  
  17. (2, 4) 
  18.  
  19. >>> porint=POINT(y=2) 
  20.  
  21. >>> porint 
  22.  
  23. <__main__.POINT object at 0x7f7be1983cb0> 
  24.  
  25. >>> point=POINT(y=2) 
  26.  
  27. >>> point.x,point.y 
  28.  
  29. (0, 2) 
  30.  
  31. 定义一个类型为POINT的数组 
  32.  
  33. >>> POINT_ARRAY=POINT*3 
  34.  
  35. >>> pa=POINT_ARRAY(POINT(2,3),POINT(2,4),POINT(2,5)) 
  36.  
  37. >>> for i in pa:print pa.y 
  38.  
  39. ... 
  40.  
  41. Traceback (most recent call last): 
  42.  
  43.   File "<stdin>", line 1, in <module> 
  44.  
  45. AttributeError: 'POINT_Array_3' object has no attribute 'y' 
  46.  
  47. >>> for i in pa:print i.y 
  48.  
  49. ... 
  50.  
  51.  
  52.  
  53.  

6.访问so文件

1.创建一个c文件

  1. #include <stdio.h> 
  2.  
  3. int hello_world(){ 
  4.  
  5.     printf("Hello World\n"); 
  6.  
  7.     return 0; 
  8.  
  9.  
  10. int main(){ 
  11.  
  12.         hello_world(); 
  13.  
  14.         return 0; 
  15.  
  16.  

2.编译成动态链接库

  1. gcc hello_world.c  -fPIC -shared -o hello_world.so 

3.python中调用库中的函数

  1. from ctypes import cdll 
  2.  
  3. c_lib=cdll.LoadLibrary('./hello_world.so'
  4.  
  5. c_lib.hello_world()  

二.测试c的性能和python的差别

sum.c

  1. int sum(int num){ 
  2.  
  3.     long sum=0; 
  4.  
  5.     int i =0; 
  6.  
  7.     for( i=1;i<=num;i++){ 
  8.  
  9.         sum=sum+i; 
  10.  
  11.     }; 
  12.  
  13.     return sum
  14.  
  15.  
  16. int main(){ 
  17.  
  18.     printf("%d",sum(10)); 
  19.  
  20.     return 0; 
  21.  
  22.  
  • 测试方案:计算1-100的和
  • 测试次数:100万次

1. 直接用c来执行,通linux 的time命令来记录执行的用时

sum.c:

  1. #include <stdio.h> 
  2.  
  3. int sum(int num){ 
  4.  
  5.     long sum=0; 
  6.  
  7.     int i =0; 
  8.  
  9.     for( i=1;i<=num;i++){ 
  10.  
  11.         sum=sum+i; 
  12.  
  13.     }; 
  14.  
  15.     return sum
  16.  
  17.  
  18. int main(){ 
  19.  
  20.     int i ; 
  21.  
  22.     for (i=0;i<1000000;i++){ 
  23.  
  24.     sum(100); 
  25.  
  26.     } 
  27.  
  28.     return 0; 
  29.  
  30.  

测试结果的例子:

  • real 1.16
  • user 1.13
  • sys 0.01

2.通过Python调用so文件和python的测试结果

sum_test.py:

  1. def sum_python(num): 
  2.  
  3.     s = 0 
  4.  
  5.     for i in xrange(1,num+1): 
  6.  
  7.         s += i 
  8.  
  9.     return s 
  10.  
  11.   
  12.  
  13.   
  14.  
  15. from ctypes import cdll 
  16.  
  17.   
  18.  
  19. c_lib = cdll.LoadLibrary('./sum.so'
  20.  
  21.   
  22.  
  23.   
  24.  
  25. def sum_c(num): 
  26.  
  27.     return c_lib.sum(num) 
  28.  
  29.   
  30.  
  31.   
  32.  
  33. def test(num): 
  34.  
  35.     import timeit 
  36.  
  37.   
  38.  
  39.     t1 = timeit.Timer('c_lib.sum(%d)' % num, 'from __main__ import c_lib'
  40.  
  41.     t2 = timeit.Timer('sum_python(%d)' % num, 'from __main__ import sum_python'
  42.  
  43.     print 'c', t1.timeit(number=1000000) 
  44.  
  45.     print 'python', t2.timeit(number=1000000) 
  46.  
  47.   
  48.  
  49.   
  50.  
  51. if __name__ == '__main__'
  52.  
  53.     test(100)  

测试结果的例子

  1. c 1.02756714821 
  2.  
  3. python 7.90672802925  

3.测试erlang的测试结果

刚刚学了erlang,那就一起测试一下erlang的运算性能

sum.erl:

  1. -module(sum). 
  2.  
  3. -export([sum/2,sum_test/2]). 
  4.  
  5. sum(0,Sum) -> 
  6.  
  7.         Sum
  8.  
  9. sum(Num,Sum) -> 
  10.  
  11.         sum(Num-1,Sum+Num). 
  12.  
  13. sum_test(Num,0) -> 
  14.  
  15.         0; 
  16.  
  17. sum_test(Num,Times) -> 
  18.  
  19.         sum(Num,0), 
  20.  
  21.         sum_test(Num,Times-1).  

调用:

  1. timer:tc(sum,sum_test,[100,1000000]). 

测试结果的例子:

  1. {2418486,0} 

4.测试结果

用上面的测试方法,进行10次测试,去除***值和最小值,再计算平均值,得出:

 

单位:秒

  • 求和的运行,使用的内存比较小,但是占用CPU资源比较多。
  • 原生的C是最快的,Python调用c会稍微慢一点,原因是计算100的和的操作是在c里面做的,而执行100万次的逻辑是在python做的
  • erlang的性能虽然比c稍慢,但是也是不错的,
  • Python的运行效率惨不忍睹。。。 
责任编辑:庞桂玉 来源: 程序源
相关推荐

2023-10-27 08:52:03

Python脚本关系

2009-08-21 17:45:40

C#调用COM对象

2016-06-14 14:50:17

Python性能

2021-04-08 10:01:40

Python模块的引入调用

2014-07-28 09:52:14

PythonPython性能

2010-03-01 16:48:02

Python模块

2010-01-11 16:31:54

C++优化器

2023-05-15 09:14:38

2010-02-01 13:25:32

Python脚本

2010-06-10 09:53:58

PythonC语言

2010-03-05 11:04:00

C调用Python函数

2018-06-14 14:07:57

Pythonweb框架

2023-03-15 15:58:11

Python动态库C++

2015-09-14 10:41:51

PHP性能分析微观分析

2015-08-18 11:44:02

PHP性能分析宏观分析

2020-06-04 12:15:08

Pythonkafka代码

2024-04-12 07:50:40

Python监控利器Time 模块

2023-12-13 09:08:26

CPU性能分析Linux

2023-06-09 12:59:52

Python性能分析

2019-07-11 10:52:02

Python统计数据
点赞
收藏

51CTO技术栈公众号