import sample print(sample.gcd(35,42)) print(sample.in_mandel(0,0,500)) print(sample.in_mandel(2.0,1.0,500)) print(sample.divide(42,8)) print(sample.avg([1,2,3])) p1 = sample.Point(1,2) p2 = sample.Point(4,5) print(sample.distance(p1,p2))
import sample print(sample.gcd(42, 8)) print(sample.divide(42, 8)) p1 = sample.Point(2, 3) p2 = sample.Point(4, 5) print(p1) print(p2) print(sample.distance(p1, p2)) import array a = array.array('d', [1, 2, 3]) print(sample.avg(a))
#!/usr/bin/env python # coding: utf-8 # setup.py from distutils.core import setup, Extension setup(name='sample', ext_modules=[ Extension('sample', ['pysample.c'], include_dirs=['/some/dir'], define_macros=[('FOO', '1')], undef_macros=['BAR'], library_dirs=['/usr/local/lib'], libraries=['sample']) ]) import sample sample.gcd(35, 42) sample.in_mandel(0, 0, 500) sample.in_mandel(2.0, 1.0, 500) sample.divide(42, 8)
#!usr/bin/env python # -*- coding:utf-8 -*- import sample print('gcd:', sample.gcd(35, 42)) print('in_mandel:', sample.in_mandel(0, 0, 500)) print('in_mandel:', sample.in_mandel(2.0, 1.0, 500)) print('divide:', sample.divide(42, 8)) print('avg:', sample.avg([1, 2, 3])) p1 = sample.Point(1, 2) p2 = sample.Point(4, 5) print('distance:', sample.distance(p1, p2))
setup(name='sample', py_modules=['sample.py'], ext_modules=[ Extension('_sample', ['sample_wrap.c'], include_dirs = [], define_macros = [], undef_macros = [], library_dirs = [], libraries = ['sample'] ) ] ) import sample sample.gcd(42,8) sample.divide(42,8) p1 = sample.Point(2,3) p2 = sample.Point(4,5) sample.distance(p1,p2) p1.x p1.y import array a = array.array('d',[1,2,3]) sample.avg(a)
import sample sample.gcd(20, 10)
import sample import array print(sample.gcd(42, 10)) print(sample.in_mandel(0, 0, 400)) print(sample.divide(42, 10)) a = array.array('d', [1, 2, 3]) print(sample.avg(a)) p1 = sample.Point(2, 3) p2 = sample.Point(4, 5) print(p1, p2) print(sample.distance(p1, p2))
import sample if __name__ == "__main__": print(sample.gcd(10, 2))
def test_capi_basics() -> None: assert 7 == sample.gcd(35, 42) assert 1 == sample.in_mandel(0., 0., 500) assert 0 == sample.in_mandel(2., 1., 500) assert (5, 2) == sample.divide(42, 8)
rem = ctypes.c_int() quot = _divide(x,y,rem) return quot,rem.value #call Point struct class Point(ctypes.Structure): _fields_ = [('x',ctypes.c_double), ('y',ctypes.c_double)] distince = _mod.distince distince.argtypes = (ctypes.POINTER(Point),ctypes.POINTER(Point)) distince.restypes = ctypes.c_double if __name__ == '__main__': print ('call c func begin') result = sample.gcd(35,42) print ('call gcd fun :result =',result) print ("-----------") in_result = sample.in_mandel(0,0,500) print ('call in_mandel :in_result = ',in_result) print ('---------') remainder,result = sample.divide(42,8) print('call divide,remainder = %d,reslut = %d' %(remainder,result)) print ('--------') p1 = sample.Point(1,2) p2 = sample.Point(4,5) dresult = sample.distince(p1,p2)
import sample print("sample.gcd(35,42) is ", sample.gcd(35,42)) print("sample.in_mandel(0,0,500) is", sample.in_mandel(0,0,500)) print("sample.divide(23,5) is", sample.divide(23,5))
from Cython.Distutils import build_ext ext_modules = [ Extension('sample', ['sample.pyx'], libraries=['sample'], library_dirs=['.'])] setup( name = 'Sample extension module', cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules ) import sample sample.gcd(42,10) sample.in_mandel(1,1,400) sample.in_mandel(0,0,400) sample.divide(42,10) import array a = array.array('d',[1,2,3]) sample.avg(a) p1 = sample.Point(2,3) p2 = sample.Point(4,5) p1
C 10000 次循环,Python 调用: 0.0003681182861328125 """ import time import sample def calc_pi(n): '''http://www.pi314.net/eng/bellard.php''' pi = 0.0 for i in range(int(n + 1)): pi += (-1)**i / 2**(10 * i) * (-32 / (4 * i + 1) - 1 / (4 * i + 3) + 256 / (10 * i + 1) - 64 / (10 * i + 3) - 4 / (10 * i + 5) - 4 / (10 * i + 7) + 1 / (10 * i + 9)) pi *= 1 / 64 return pi if __name__ == '__main__': sample.gcd(35, 7) sample.divide(42, 8) now = time.time() loop_time = 10000 print(calc_pi(loop_time)) print('纯 Python ' + str(loop_time) + ' 次循环 :', (time.time() - now)) now = time.time() print(sample.calc_pi(int(loop_time))) print('C ' + str(loop_time) + ' 次循环,Python 调用:', (time.time() - now))