日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python調(diào)用C模塊以及性能分析

一.c,ctypes和python的數(shù)據(jù)類型的對應(yīng)關(guān)系

創(chuàng)新互聯(lián)建站專注于長興企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都做商城網(wǎng)站。長興網(wǎng)站建設(shè)公司,為長興等地區(qū)提供建站服務(wù)。全流程專業(yè)公司,專業(yè)設(shè)計,全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

  • 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. >>> c=c_int(34)
  3. >>> c
  4. c_int(34)
  5. >>> c.value
  6. 34
  7. >>> c.value=343
  8. >>> c.value
  9. 343 

3.操作字符串

 
 
 
 
  1. >>> p=create_string_buffer(10)
  2. >>> p.raw
  3. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  4. >>> p.value='fefefe'
  5. >>> p.raw
  6. 'fefefe\x00\x00\x00\x00'
  7. >>> p.value='fefeeeeeeeeeeeeeeeeeeeeeee'  #字符串太長,報錯
  8. Traceback (most recent call last):
  9.   File "", line 1, in 
  10. ValueError: string too long 

4.操作指針

 
 
 
 
  1. >>> i=c_int(999)
  2. >>> pi=pointer(i)
  3. >>> pi
  4. <__main__.LP_c_int object at 0x7f7be1983b00>
  5. >>> pi.value
  6. Traceback (most recent call last):
  7.   File "", line 1, in 
  8. AttributeError: 'LP_c_int' object has no attribute 'value'
  9. >>> pi.contents
  10. c_int(999)
  11. >>> pi.contents=c_long(34343)
  12. >>> pi.contents
  13. c_int(34343) 
  • 通過pointer獲取一個值的指針
  • 通過contents獲取一個指針的值

5.c的結(jié)構(gòu)體

 
 
 
 
  1. #定義一個c的structure,包含兩個成員變量x和y
  2. >>> class POINT(Structure):
  3. ...     _fields_=[('x',c_int),('y',c_int)]
  4. ...
  5. >>> point=POINT(2,4)
  6. >>> point
  7. <__main__.POINT object at 0x7f7be1983b90>
  8. >>> point.x,point.y
  9. (2, 4)
  10. >>> porint=POINT(y=2)
  11. >>> porint
  12. <__main__.POINT object at 0x7f7be1983cb0>
  13. >>> point=POINT(y=2)
  14. >>> point.x,point.y
  15. (0, 2)
  16. 定義一個類型為POINT的數(shù)組
  17. >>> POINT_ARRAY=POINT*3
  18. >>> pa=POINT_ARRAY(POINT(2,3),POINT(2,4),POINT(2,5))
  19. >>> for i in pa:print pa.y
  20. ...
  21. Traceback (most recent call last):
  22.   File "", line 1, in 
  23. AttributeError: 'POINT_Array_3' object has no attribute 'y'
  24. >>> for i in pa:print i.y
  25. ...
  26. 3
  27. 4

6.訪問so文件

1.創(chuàng)建一個c文件

 
 
 
 
  1. #include 
  2. int hello_world(){
  3.     printf("Hello World\n");
  4.     return 0;
  5. }
  6. int main(){
  7.         hello_world();
  8.         return 0;

2.編譯成動態(tài)鏈接庫

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

3.python中調(diào)用庫中的函數(shù)

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

二.測試c的性能和python的差別

sum.c

 
 
 
 
  1. int sum(int num){
  2.     long sum=0;
  3.     int i =0;
  4.     for( i=1;i<=num;i++){
  5.         sum=sum+i;
  6.     };
  7.     return sum;
  8. }
  9. int main(){
  10.     printf("%d",sum(10));
  11.     return 0;
  • 測試方案:計算1-100的和
  • 測試次數(shù):100萬次

1. 直接用c來執(zhí)行,通linux 的time命令來記錄執(zhí)行的用時

sum.c:

 
 
 
 
  1. #include 
  2. int sum(int num){
  3.     long sum=0;
  4.     int i =0;
  5.     for( i=1;i<=num;i++){
  6.         sum=sum+i;
  7.     };
  8.     return sum;
  9. }
  10. int main(){
  11.     int i ;
  12.     for (i=0;i<1000000;i++){
  13.     sum(100);
  14.     }
  15.     return 0;

測試結(jié)果的例子:

  • real 1.16
  • user 1.13
  • sys 0.01

2.通過Python調(diào)用so文件和python的測試結(jié)果

sum_test.py:

 
 
 
 
  1. def sum_python(num):
  2.     s = 0
  3.     for i in xrange(1,num+1):
  4.         s += i
  5.     return s
  6.  
  7.  
  8. from ctypes import cdll
  9.  
  10. c_lib = cdll.LoadLibrary('./sum.so')
  11.  
  12.  
  13. def sum_c(num):
  14.     return c_lib.sum(num)
  15.  
  16.  
  17. def test(num):
  18.     import timeit
  19.  
  20.     t1 = timeit.Timer('c_lib.sum(%d)' % num, 'from __main__ import c_lib')
  21.     t2 = timeit.Timer('sum_python(%d)' % num, 'from __main__ import sum_python')
  22.     print 'c', t1.timeit(number=1000000)
  23.     print 'python', t2.timeit(number=1000000)
  24.  
  25.  
  26. if __name__ == '__main__':
  27.     test(100) 

測試結(jié)果的例子

 
 
 
 
  1. c 1.02756714821
  2. python 7.90672802925 

3.測試erlang的測試結(jié)果

剛剛學(xué)了erlang,那就一起測試一下erlang的運(yùn)算性能

sum.erl:

 
 
 
 
  1. -module(sum).
  2. -export([sum/2,sum_test/2]).
  3. sum(0,Sum) ->
  4.         Sum;
  5. sum(Num,Sum) ->
  6.         sum(Num-1,Sum+Num).
  7. sum_test(Num,0) ->
  8.         0;
  9. sum_test(Num,Times) ->
  10.         sum(Num,0),
  11.         sum_test(Num,Times-1). 

調(diào)用:

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

測試結(jié)果的例子:

 
 
 
 
  1. {2418486,0}

4.測試結(jié)果

用上面的測試方法,進(jìn)行10次測試,去除***值和最小值,再計算平均值,得出:

單位:秒

  • 求和的運(yùn)行,使用的內(nèi)存比較小,但是占用CPU資源比較多。
  • 原生的C是最快的,Python調(diào)用c會稍微慢一點(diǎn),原因是計算100的和的操作是在c里面做的,而執(zhí)行100萬次的邏輯是在python做的
  • erlang的性能雖然比c稍慢,但是也是不錯的,
  • Python的運(yùn)行效率慘不忍睹。。。

分享標(biāo)題:Python調(diào)用C模塊以及性能分析
文章鏈接:http://www.dlmjj.cn/article/dheeiph.html