现在的位置: 首页 > 综合 > 正文

在python中嵌入c/c++

2013年02月18日 ⁄ 综合 ⁄ 共 2326字 ⁄ 字号 评论关闭
   学习python是一个令人振奋不已的过程,python是一个如此powerfull的高级语言,简单却功能强大,库多而又功能齐全,几乎可以帮助我们完成任何一项工作。它唯一的缺陷就是跑得慢,在跑得慢的问题上,它有有着令人振奋的解决方案,嵌入c/c++代码的方法。一个程序80%的时间运行在20%的代码上,我们只要用c重写那20%的代码,便可缔造完美程序。
   除了运行速度问题,还有其他可以用c来增强的功能。如python2.5可递归栈的最大层次是999,而c却远远大于这个限制。

 exmaple.c

  1. /*
  2.  * =============================================================================
  3. ========
  4.  *
  5.  *       Filename:  example.c
  6.  *
  7.  *    Description:  embed c in python
  8.  *
  9.  *        Version:  1.0
  10.  *        Created:  2008年12月15日 20时48分19秒
  11.  *       Revision:  none
  12.  *       Compiler:  gcc
  13.  *
  14.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  15.  *        Company:  hunan university
  16.  *
  17.  * =============================================================================
  18. ========
  19.  */
  20. int fact(int n)
  21. {
  22.     if(n <= 1)
  23.         return 1;
  24.     else
  25.         return n*fact(n-1);
  26. }

wrap.c

  1. /*
  2.  * =============================================================================
  3. ========
  4.  *
  5.  *       Filename:  wrap.c
  6.  *
  7.  *    Description:  c and python
  8.  *
  9.  *        Version:  1.0
  10.  *        Created:  2008年12月15日 20时49分11秒
  11.  *       Revision:  none
  12.  *       Compiler:  gcc
  13.  *
  14.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  15.  *        Company:  hunan university
  16.  *
  17.  * =============================================================================
  18. ========
  19.  */
  20. #include<Python.h>
  21. //导出函数
  22. PyObject *wrap_fact(PyObject* self,PyObject* args)
  23. {
  24.     int n,result;
  25.     if (!PyArg_ParseTuple(args,"i:fact",&n))
  26.         return NULL;
  27.     result=fact(n);
  28.     return Py_BuildValue("i",result);
  29. }
  30. //方法列表
  31. static PyMethodDef exampleMethods[]=
  32. {
  33.     {"fact",wrap_fact,METH_VARARGS,"Caculate N!"},
  34.     {NULL,NULL}
  35. };
  36. //初始化函数
  37. void initexample()
  38. {
  39.     PyObject* m;
  40.     m=Py_InitModule("example",exampleMethods);
  41. }

编译

gcc -fpic -c -I/usr/include/python2.5 /
-I /usr/lib/python2.5/config /
example.c wrapper.c

gcc -shared -o example.so example.o wrapper.o

fact.py

  1. import example
  2. import datetime
  3. def fact(n):
  4.     if n<=1:return 1
  5.     else:return n*fact(n-1)
  6. if __name__=='__main__':
  7.     now=datetime.datetime.now()
  8.     for i in range(10000):
  9.         n=fact(100)
  10.     end=datetime.datetime.now()
  11.     print 'the python fact takes:',end-now
  12.     for i in range(10000):
  13.         n=example.fact(100)
  14.     print 'the c fact taks:',datetime.datetime.now()-end

运行结果:
lwj@lwj-desktop:~/code/python$   python fact.py
the python fact takes: 0:00:01.214449
the c fact taks: 0:00:00.121041

c要快了》10倍。不过还没测试过单纯的c的环境。。。有空再测测。。。。
另外python本身只能递归999层深度
c没有这个限制

抱歉!评论已关闭.