作家
登录

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

作者: 来源: 2017-07-13 14:05:08 阅读 我要评论

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

sum_test.py:

2.操作int

1.创建一个c文件

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=http://developer.51cto.com/art/201707/'fefefe' 
  8.  
  9. >>> p.raw 
  10.  
  11. 'fefefe\x00\x00\x00\x00' 
  12.  
  13. >>> p.value=http://developer.51cto.com/art/201707/'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): 
     1/5    1 2 3 4 5 下一页 尾页

      推荐阅读

      爱加密与安百科技深度“联姻” 共同消弭企业“安全孤岛”

    平顺家当涵盖的范畴异常广泛,固然每个细分市场都有“头牌”,但并没有某一家安然巨擘有底气传播鼓>>>详细阅读


    本文标题:Python调用C模块以及性能分析

    地址:http://www.17bianji.com/lsqh/36195.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)