swig-python

转载自:http://blog.csdn.net/king_on/article/details/8092399

1. SWIG介绍(来自于wiki)

SWIG (Simplified Wrapper and Interface Generator) is anopen source software tool used to connectcomputer programs or libraries written in C or C++ with scripting languages such as LuaPerlPHPPythonR,RubyTcl, and other languages likeC#JavaGoModula-3,OCamlOctave, and Scheme. Output can also be in the form of XML or Lisp S-expressions.

2.SWIG的安装

2.1 下载地址: http://www.swig.org/download.html

2.2 将下载的文件如swig-2.0.8.tar.gz解压

2.3 进入swig-2.0.8目录,执行一下命令

[plain]  view plain  copy
  1. ./configure --prefix=/usr/program/swig/ #或者其他安装目录  
  2. make  
  3. make install  
在./configure 时遇到Cannot find pcre-config script from PCRE (Perl Compatible Regular Expressions)的错误,使用

./configure --prefix=/usr/progam/swig --without-pcre, 因为我使用swig的目的是Python

3.SWIG的使用

SWIG支持多种语言(脚本),这里介绍python的一个例子,这个例子是使用c语言模拟位数组,然后交由python扩展

最后有一个使用c扩展python和直接使用python的效率比较

1. 编写c文件

[cpp]  view plain  copy
  1. //Bit.h  
  2. #ifndef BIT_H  
  3. #define BIT_H  
  4.   
  5. //create: 2012-10-19  
  6. //version: 1.0  
  7. #define CHAR_LENGTH sizeof(unsigned char)   
  8.   
  9. //Bit模拟位操作  
  10. typedef struct  
  11. {  
  12.     unsigned char *pArray;//指向一个字符型数组,用以存储bit位  
  13.     unsigned long length;//记录pArray的长度, 字符个数  
  14.     unsigned long used;//记录用户使用的bit位  
  15. }Bit;  
  16.   
  17. //创建Bit对象  
  18. //@len: 初始化bit位数  
  19. //@return: 返回一个指向Bit对象的指针, 该指针需要使用freeBit销毁  
  20. Bit* createBit(unsigned long len);  
  21.   
  22. //设置bit位  
  23. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  24. //@index: 需要设置的bit位, 范围应当是 (0~used)  
  25. //@value: bit位的值, (0,1)  
  26. //@return: 成功返回0, 如果出现pb==NULL, index out of range, value!=0 and value!=1, 返回1  
  27. int setBit(Bit *pb,unsigned long index, int value);  
  28.   
  29. //获取bit位  
  30. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  31. //@index: 需要设置的bit位, 范围应当是 (0~used)  
  32. //@return: 成功返回0, 如果出现pb==NULL, index out of range, 返回1  
  33. int getBit(Bit *pb,unsigned long index);  
  34.   
  35. //返回使用的长度  
  36. //@pb: 如果pb==NULL, return -1  
  37. int bitLength(Bit *pb);  
  38.   
  39. //销毁Bit对象  
  40. void freeBit(Bit *pb);  
  41. #endif  
[cpp]  view plain  copy
  1. //Bit.c  
  2. #include "Bit.h"  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <limits.h>  
  6.   
  7. //创建Bit对象  
  8. //@len: 初始化bit位数, len>=0  
  9. //@return: 返回一个指向Bit对象的指针, 该指针需要使用freeBit销毁  
  10. Bit* createBit(unsigned long len)  
  11. {  
  12.     if(len<0)  
  13.         return NULL;  
  14.   
  15.     Bit *pb=(Bit*)malloc(sizeof(Bit));  
  16.     if(len>0)  
  17.     {  
  18.         pb->length=(len-1)/CHAR_LENGTH+1;  
  19.         pb->pArray=(char*)malloc(sizeof(char)*pb->length);  
  20.         pb->used=len;  
  21.     }else  
  22.     {  
  23.         pb->length=0;  
  24.         pb->pArray=NULL;  
  25.         pb->used=len;  
  26.     }  
  27.     return pb;  
  28. }  
  29.   
  30. //设置bit位  
  31. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  32. //@index: 需要设置的bit位, 范围应当是 [0~used)  
  33. //@value: bit位的值, (0,1)  
  34. //@return: 成功返回0, 如果出现pb==NULL, index out of range, value!=0 and value!=1, 返回1  
  35. int setBit(Bit *pb,unsigned long index, int value)  
  36. {  
  37.     if(pb==NULL || pb->pArray==NULL || index<0 || index>=pb->used || (value!=0 && value!=1))  
  38.         return 1;  
  39.   
  40.     unsigned long a=index/CHAR_LENGTH;  
  41.     unsigned long b=index%CHAR_LENGTH;  
  42.     if(value==0)  
  43.     {  
  44.         pb->pArray[a]&=(UCHAR_MAX^(1<<b));  
  45.         //printf("%d\n",pb->pArray[a]);  
  46.     }else  
  47.     {  
  48.         pb->pArray[a]|=(1<<b);  
  49.         //printf("%d\n",pb->pArray[a]);  
  50.     }  
  51.     return 0;  
  52. }  
  53.   
  54. //获取bit位  
  55. //@pb:指向Bit的一个对象, 如果为NULL, 返回1  
  56. //@index: 需要设置的bit位, 范围应当是 [0~used)  
  57. //@return: 成功返回0, 如果出现pb==NULL, index out of range, 返回1  
  58. int getBit(Bit *pb,unsigned long index)  
  59. {  
  60.     if(pb==NULL || pb->pArray==NULL || index<0 || index>=pb->used)  
  61.         return 1;  
  62.     unsigned long a=index/CHAR_LENGTH;  
  63.     unsigned long b=index%CHAR_LENGTH;  
  64.     //printf("%d\n",pb->pArray[a]&(1<<b));  
  65.     if((pb->pArray[a]&(1<<b))==0)  
  66.         return 0;  
  67.     else  
  68.         return 1;  
  69. }  
  70.   
  71. //返回使用的长度  
  72. //@pb: 如果pb==NULL, return -1  
  73. int bitLength(Bit *pb)  
  74. {  
  75.     if(pb==NULL || pb->pArray==NULL)  
  76.         return -1;  
  77.     return pb->used;  
  78. }  
  79.   
  80. //销毁Bit对象  
  81. void freeBit(Bit *pb)  
  82. {  
  83.     if(pb==NULL)  
  84.         return;  
  85.     if(pb->pArray!=NULL)  
  86.     {  
  87.         free(pb->pArray);  
  88.         pb->pArray=NULL;  
  89.     }  
  90.     free(pb);  
  91. }  

2. 编写SWIG使用的swg文件

//Bit.i

[plain]  view plain  copy
  1. %module Bit #module name  
  2. %{  
  3. #include "Bit.h" #加入Bit_wrap.c文件  
  4. %}  
  5.   
  6. #需要导出到python的函数  
  7. extern Bit* createBit(unsigned long len);  
  8. extern int setBit(Bit *pb,unsigned long index, int value);  
  9. extern int getBit(Bit *pb,unsigned long index);  
  10. extern int bitLength(Bit *pb);  
  11. extern void freeBit(Bit *pb);  

3. 编译

3.1 使用Bit.i生成wrap文件,该命令得到Bit_wrap.c Bit.py

[plain]  view plain  copy
  1. [username]$ swig -python Bit.i  
3.2 编译Bit.c Bit_wrap.c文件,其中PYTHON_INCLUDE为python安装目录下的include文件夹, 如/usr/program/python/include/python.2.7username]$ gcc -c

[plain]  view plain  copy
  1. [username]$ gcc -c -fpic Bit.c Bit_wrap.c -I${PYTHON_INCLUDE}  
3.3 连接得到动态库

[plain]  view plain  copy
  1. [username]$ gcc -shared Bit.o Bit_wrap.o -o _Bit.so  
注意输出库文件为_Bit.so, 有下划线和模块名称组成
4.使用,这里以计算一亿一下的素数个数来演示

将Bit.py和_Bit.so文件复制到需要的位置

编写prime_count.py

[python]  view plain  copy
  1. #coding=utf-8  
  2. #create-2012-10-17  
  3. #version: 1.0  
  4. #计算小于1亿的素数个数  
  5.   
  6. import time  
  7. from Bit  import *  
  8.   
  9. MAX=100000000  
  10. count=1  
  11.   
  12. #0 表示素数  
  13. #1 表示已经去除  
  14.   
  15. start_time=time.time()  
  16.   
  17. len=MAX/2-1  
  18. b=createBit(len)  
  19.   
  20. def remove(idx):  
  21.     i=idx  
  22.     global b  
  23.     while i<len:  
  24.         #b[i]=1  
  25.         setBit(b,i,1)  
  26.         i+=2*idx+3  
  27.   
  28. for i in range(3,MAX,2):  
  29.     if getBit(b,(i-3)/2)==0:  
  30.         #素数  
  31.         count+=1  
  32.         remove((i-3)/2)  
  33.   
  34. end_time=time.time()  
  35. print count  
  36. print end_time-start_time  
使用方法和其他python提供的模块一样

5.附计算结果

使用以上方法(c扩展python)得到结果为:

[html]  view plain  copy
  1. 5761455(素数个数)  
  2. 113.465720892(second)  
另外,如果直接使用python模拟bit,并同样计算一亿一下的素数个数,结果为

[plain]  view plain  copy
  1. 5761455(素数个数)  
  2. 293.473055124(second)  
可以看到,性能提升非常明显(接近3倍),当然这只是一个例子,并不具有太多代表性

另附,同样的方法,如果是c语言,调用我们实现的Bit的话结果为:

[plain]  view plain  copy
  1. 5761455(素数个数)  
  2. 5(second)  

计算这种事情还是交给c做比较恰当

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值