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

cellyhu 4年前
   <h3>1.c,ctypes和python的数据类型的对应关系</h3>    <p>ctypes type ctype Python type</p>    <p>c_char char 1-character string</p>    <p>c_wchar wchar_t 1-character unicode string</p>    <p>c_byte char int/long</p>    <p>c_ubyte unsigned char int/long</p>    <p>c_short short int/long</p>    <p>c_ushort unsigned short int/long</p>    <p>c_int int int/long</p>    <p>c_uint unsigned int int/long</p>    <p>c_long long int/long</p>    <p>c_ulong unsigned long int/long</p>    <p>c_longlong __int64 or long long int/long</p>    <p>c_ulonglong unsigned __int64 or unsigned long long int/long</p>    <p>c_float float float</p>    <p>c_double double float</p>    <p>c_char_p char * (NUL terminated) string or None</p>    <p>c_wchar_p wchar_t * (NUL terminated) unicode or None</p>    <p>c_void_p void * int/long or None</p>    <h3>2.操作int</h3>    <pre>  <code class="language-python">>>> fromctypesimport *  >>> c=c_int(34)  >>> c  c_int(34)  >>> c.value  34  >>> c.value=343  >>> c.value  343  </code></pre>    <h3>3.操作字符串</h3>    <pre>  <code class="language-python">>>> 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>", line 1, in <module>  ValueError: string toolong  </code></pre>    <h3>4.操作指针</h3>    <pre>  <code class="language-python">>>> i=c_int(999)  >>> pi=pointer(i)  >>> pi  <__main__.LP_c_intobject at 0x7f7be1983b00>  >>> pi.value  Traceback (mostrecentcalllast):    File "<stdin>", line 1, in <module>  AttributeError: 'LP_c_int' object hasnoattribute 'value'  >>> pi.contents  c_int(999)  >>> pi.contents=c_long(34343)  >>> pi.contents  c_int(34343)  </code></pre>    <ul>     <li>通过pointer获取一个值的指针</li>     <li>通过contents获取一个指针的值</li>    </ul>    <h3>5.c的结构体</h3>    <pre>  <code class="language-python">#定义一个c的structure,包含两个成员变量x和y  >>> class POINT(Structure):  ...    _fields_=[('x',c_int),('y',c_int)]  ...   >>> point=POINT(2,4)  >>> point  <__main__.POINTobject at 0x7f7be1983b90>  >>> point.x,point.y  (2, 4)  >>> porint=POINT(y=2)  >>> porint  <__main__.POINTobject at 0x7f7be1983cb0>  >>> 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))  >>> for i in pa:print pa.y  ...   Traceback (mostrecentcalllast):    File "<stdin>", line 1, in <module>  AttributeError: 'POINT_Array_3' object hasnoattribute 'y'  >>> for i in pa:print i.y   ...   3  4  5  </code></pre>    <h3>6.访问so文件</h3>    <p>1.创建一个c文件</p>    <pre>  <code class="language-python">#include <stdio.h>  int hello_world(){      printf("Hello World\n");      return 0;  }  int main(){          hello_world();          return 0;  }  </code></pre>    <p>2.编译成动态链接库</p>    <pre>  <code class="language-python">gcchello_world.c  -fPIC -shared -o hello_world.so  </code></pre>    <p>3.python中调用库中的函数</p>    <pre>  <code class="language-python">fromctypesimportcdll  c_lib=cdll.LoadLibrary('./hello_world.so')  c_lib.hello_world()  </code></pre>    <h2>二.测试c的性能和python的差别</h2>    <p>sum.c</p>    <p>#include</p>    <pre>  <code class="language-python">int sum(int num){      long sum=0;      int i =0;      for( i=1;i<=num;i++){          sum=sum+i;      };      return sum;  }  int main(){      printf("%d",sum(10));      return 0;  }  </code></pre>    <ul>     <li>测试方案:计算1-100的和</li>     <li>测试次数:100万次 <h3>1. 直接用c来执行,通linux 的time命令来记录执行的用时</h3> <p>sum.c:</p> <pre>  <code class="language-python">#include <stdio.h>  int sum(int num){      long sum=0;      int i =0;      for( i=1;i<=num;i++){          sum=sum+i;      };      return sum;  }  int main(){      int i ;      for (i=0;i<1000000;i++){      sum(100);      }      return 0;  </code></pre> <p>测试结果的例子:</p> <p>real 1.16</p> <p>user 1.13</p> <p>sys 0.01</p> </li>    </ul>    <h3>2.通过Python调用so文件和python的测试结果</h3>    <p>sum_test.py:</p>    <pre>  <code class="language-python">defsum_python(num):      s = 0      for i in xrange(1,num+1):          s += i      return s        fromctypesimportcdll     c_lib = cdll.LoadLibrary('./sum.so')        defsum_c(num):      return c_lib.sum(num)        deftest(num):      importtimeit         t1 = timeit.Timer('c_lib.sum(%d)' % num, 'from __main__ import c_lib')      t2 = timeit.Timer('sum_python(%d)' % num, 'from __main__ import sum_python')      print 'c', t1.timeit(number=1000000)      print 'python', t2.timeit(number=1000000)        if __name__ == '__main__':      test(100)  </code></pre>    <p>测试结果的例子</p>    <pre>  <code class="language-python">c 1.02756714821  python 7.90672802925  </code></pre>    <h3>3.测试erlang的测试结果</h3>    <p>刚刚学了erlang,那就一起测试一下erlang的运算性能</p>    <p>sum.erl:</p>    <pre>  <code class="language-python">-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).            </code></pre>    <p>调用:</p>    <pre>  <code class="language-python">timer:tc(sum,sum_test,[100,1000000]).  </code></pre>    <p>测试结果的例子:</p>    <pre>  <code class="language-python">{2418486,0}  </code></pre>    <h3>4.测试结果</h3>    <p>用上面的测试方法,进行10次测试,去除最大值和最小值,再计算平均值,得出:</p>    <table>     <thead>      <tr>       <th>Python调用c</th>       <th>原生的c</th>       <th>Python</th>       <th>erlang</th>      </tr>     </thead>     <tbody>      <tr>       <td>0.95</td>       <td>0.48</td>       <td>8.47</td>       <td>2.43</td>      </tr>     </tbody>    </table>    <p>单位:秒</p>    <ul>     <li>求和的运行,使用的内存比较小,但是占用CPU资源比较多。</li>     <li>原生的C是最快的,Python调用c会稍微慢一点,原因是计算100的和的操作是在c里面做的,而执行100万次的逻辑是在python做的</li>     <li>erlang的性能虽然比c稍慢,但是也是不错的,</li>     <li>Python的运行效率惨不忍睹。。。</li>    </ul>    <p> </p>    <p>来自:http://python.jobbole.com/87044/</p>    <p> </p>