From 0dd6a74a3a41851e2b4cc92a7d2bd3c858ee4cf8 Mon Sep 17 00:00:00 2001 From: Arseniy Antonov Date: Wed, 31 Jan 2018 18:03:05 +0300 Subject: [PATCH 1/4] Added cython to the requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e302695..207a964 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pytest==3.0.3 pytest-cov==2.5.1 python-coveralls==2.9.1 +Cython==0.27.3 From 7756530d5f9d50d4848b913f9b36fd2b471e4a98 Mon Sep 17 00:00:00 2001 From: Arseniy Antonov Date: Wed, 31 Jan 2018 18:03:21 +0300 Subject: [PATCH 2/4] Added basic setup file --- setup.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3dda5ca --- /dev/null +++ b/setup.py @@ -0,0 +1,29 @@ +import os + +from Cython.Build import cythonize +from setuptools import setup, Extension + + +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + + +ext_modules = [Extension("algorithms.arithmetic.fib", ["algorithms/arithmetic/fib.pyx"])] +setup( + name="python_algorithms", + version="0.0.1", + author="Arseniy Antonov", + author_email="arseny.antonov@gmail.com", + description=("Python algorithms"), + license="MIT", + keywords="algorithms graph arithmetic cython", + packages=['algorithms', 'tests'], + long_description=read('README.rst'), + ext_modules=cythonize(ext_modules), + classifiers=[ + "Development Status :: 3 - Alpha", + "Topic :: Utilities", + "License :: OSI Approved :: MIT License", + ], + +) From 67a1dcf85ddbe028d9e4c8780efcd8f94b0d6dad Mon Sep 17 00:00:00 2001 From: Arseniy Antonov Date: Wed, 31 Jan 2018 18:03:55 +0300 Subject: [PATCH 3/4] Added python and cython fibonnaci implementations --- algorithms/arithmetic/fib.pyx | 6 ++++++ algorithms/arithmetic/fibonacci.py | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 algorithms/arithmetic/fib.pyx create mode 100644 algorithms/arithmetic/fibonacci.py diff --git a/algorithms/arithmetic/fib.pyx b/algorithms/arithmetic/fib.pyx new file mode 100644 index 0000000..00ba2e5 --- /dev/null +++ b/algorithms/arithmetic/fib.pyx @@ -0,0 +1,6 @@ +cpdef long long fib_fast(long long n): + if n <= 1: + return n + + return fib_fast(n - 1) + fib_fast(n - 2) + diff --git a/algorithms/arithmetic/fibonacci.py b/algorithms/arithmetic/fibonacci.py new file mode 100644 index 0000000..b0f1d1f --- /dev/null +++ b/algorithms/arithmetic/fibonacci.py @@ -0,0 +1,8 @@ +# Uses python3 +def fib_recursive(n): + if n <= 1: + return n + + return fib_recursive(n - 1) + fib_recursive(n - 2) + + From 66d458e035a914aa4aa92652d0647977f352b4db Mon Sep 17 00:00:00 2001 From: Arseniy Antonov Date: Wed, 31 Jan 2018 19:05:14 +0300 Subject: [PATCH 4/4] Fibonacci rewritten from pyx to plain C --- algorithms/arithmetic/fib.c | 40 +++++++++++++++++++++++++++++++++++ algorithms/arithmetic/fib.pyx | 6 ------ setup.py | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 algorithms/arithmetic/fib.c delete mode 100644 algorithms/arithmetic/fib.pyx diff --git a/algorithms/arithmetic/fib.c b/algorithms/arithmetic/fib.c new file mode 100644 index 0000000..7c111ea --- /dev/null +++ b/algorithms/arithmetic/fib.c @@ -0,0 +1,40 @@ + +#include + +long long +_fib(long long n) { + if (n < 2) + return n; + else + return _fib(n - 1) + _fib(n - 2); +} + +static PyObject * +fib(PyObject *self, PyObject *args) { + long long n; + if (!PyArg_ParseTuple(args, "L", &n)) + return NULL; + return Py_BuildValue("L", _fib(n)); + +} + +static PyMethodDef fib_methods[] = { + { + "fib_fast", fib, METH_VARARGS, "Calcs fib." + }, + { NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef fib_def = + { + PyModuleDef_HEAD_INIT, + "algorithms.arithmetic.fib", /* name of module */ + "", /* module documentation, may be NULL */ + -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ + fib_methods + }; + +PyMODINIT_FUNC PyInit_fib(void) { + Py_Initialize(); + return PyModule_Create(&fib_def); +} \ No newline at end of file diff --git a/algorithms/arithmetic/fib.pyx b/algorithms/arithmetic/fib.pyx deleted file mode 100644 index 00ba2e5..0000000 --- a/algorithms/arithmetic/fib.pyx +++ /dev/null @@ -1,6 +0,0 @@ -cpdef long long fib_fast(long long n): - if n <= 1: - return n - - return fib_fast(n - 1) + fib_fast(n - 2) - diff --git a/setup.py b/setup.py index 3dda5ca..af4da12 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() -ext_modules = [Extension("algorithms.arithmetic.fib", ["algorithms/arithmetic/fib.pyx"])] +ext_modules = [Extension("algorithms.arithmetic.fib", ["algorithms/arithmetic/fib.c"])] setup( name="python_algorithms", version="0.0.1",