שיאון שחוריMilOSS-il 2012. מוטיבציה  python זה קל ו C זה מהיר. למה לא לשלב?  יש כבר קוד קיים ב C. אנחנו רוצים להשתמש בו, ולבסס מעליו קוד חדש ב python

  • View
    219

  • Download
    0

Embed Size (px)

Text of שיאון שחוריMilOSS-il 2012. מוטיבציה  python זה קל ו C זה מהיר....

MilOSS-il 2012'//"#'//"#1python C . ? C. , python

: ( ) python C

(): , ctypes, C/Python API cython'//"#'//"#2 () C, python IO (socket, ') ? C '//"#'//"#3 (ctypes) dll ctypes dynamic library C ctypes C'//"#'//"#4 (ctypes) ? ( ) ( python C )

'//"#'//"#5 (C/Python API) C/Python API C Python.h extension distutils import ? (, C)

'//"#'//"#6 (Cython) Cython pyrex .pyx: python ( ) extension distutils import ? ( )

'//"#'//"#7Cython

#include

inline int my_func(int x){ printf(my func %d\n, x); return 0;}

cdef extern from bla.h: int my_func(int) def pymy_func(x): return my_func(x)

import blaprint bla.pymy_func(10)bla.hbla.pyxmain.py'//"#'//"#8Distutils setup.py

from distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_ext

setup(cmdclass={'build_ext': build_ext}, ext_modules=[Extension(bla", ['bla.pyx'])])setup.py

$ python setup.py build_ext --inplacerunning build_extcythoning bla.pyx to bla.cbuilding 'bla' extensiongcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c bla.c -o build/temp.linux-i686-2.6/bla.ogcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-i686-2.6/bla.o -o /mnt/filer_home/users/t_sion/bla.so'//"#'//"#9: : , : : not '//"#'//"#10

import array

lookup = {}

for i in range(256): lookup[chr(i)] = chr(i) + chr(i ^ 0xFF)

def encode(arr): out = array.array('c', '\x00' * len(arr) * 2) for i in xrange(len(arr)): data = lookup[arr[i]] out[2*i] = data[0] out[2*i + 1] = data[1]

return outpyencode.py'//"#'//"#11 -

In [10]: s = array.array('c', '\x01\xf3\x34\x45' * (10 ** 6))

In [11]: import pyencode

In [12]: %timeit -n 3 pyencode.encode(s)3 loops, best of 3: 2.84 s per loop'//"#'//"#12 cython

In [13]: import cyencode_simple

In [14]: %timeit -n 3 cyencode_simple.encode(s)3 loops, best of 3: 1.62 s per loop , cython cyencode_simple '//"#'//"#13 cython

import arraycdef unsigned short lookup[256]cdef unsigned char *_ptrfor i in range(256): _ptr = (&lookup[i]) _ptr[0] = i _ptr[1] = (i ^ 0xFF)

def encode(arr): cdef int i cdef unsigned char *in_ptr, *out_ptr, *p in_ptr = (arr.buffer_info()[0]) out = array.array('c', '\x00' * len(arr) * 2) out_ptr = (out.buffer_info()[0]) for i in range(len(arr)): p = (&lookup[in_ptr[i]]) out_ptr[2*i] = p[0] out_ptr[2*i + 1] = p[1] return outcyencode.py'//"#'//"#14 cython -

In [15]: import cyencode

In [16]: %timeit -n 3 cyencode.encode(s)3 loops, best of 3: 26.8 ms per loop'//"#'//"#15 cython C

for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_4; __pyx_t_6+=1) { __pyx_v_i = __pyx_t_6;

/* "cyencode.pyx":17 * p = (&lookup[in_ptr[i]]) #