Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cython to speed up some code #80

Open
NardJ opened this issue Aug 1, 2018 · 6 comments
Open

Cython to speed up some code #80

NardJ opened this issue Aug 1, 2018 · 6 comments
Labels
investigate Issues that adress a hipothesis or need further insights in order to be resolved

Comments

@NardJ
Copy link
Collaborator

NardJ commented Aug 1, 2018

Stumbled accross Cython which may have some use to speed up PhotonFileEditor.

Nice comparison between Cython, CPython, PyPy and Python.

And here as simple benchmark.

@NardJ NardJ added the investigate Issues that adress a hipothesis or need further insights in order to be resolved label Aug 1, 2018
@NardJ
Copy link
Collaborator Author

NardJ commented Aug 6, 2018

@X3msnake
Copy link
Member

X3msnake commented Aug 6, 2018 via email

@NardJ
Copy link
Collaborator Author

NardJ commented Aug 6, 2018

Well I think a lot can be achieved with smarter coding and parallel processing. This should be a last resort because it would python collaborators to help with programming.

@NardJ
Copy link
Collaborator Author

NardJ commented Aug 15, 2018

Another demo calculating mandelbroth images.

@NardJ
Copy link
Collaborator Author

NardJ commented Aug 15, 2018

And from docs.cython.org
Building a Cython module using distutils¶

Imagine a simple “hello world” script in a file hello.pyx:

def say_hello_to(name):
    print("Hello %s!" % name)

The following could be a corresponding setup.py script:

from distutils.core import setup
from Cython.Build import cythonize

setup(name='Hello world app',
      ext_modules=cythonize("hello.pyx"))

To build, run python setup.py build_ext --inplace. Then simply start a Python session and do from hello import say_hello_to and use the imported function as you see fit.

One caveat if you use setuptools instead of distutils, the default action when running python setup.py install is to create a zipped egg file which will not work with cimport for pxd files when you try to use them from a dependent package. To prevent this, include zip_safe=False in the arguments to setup().

@NardJ
Copy link
Collaborator Author

NardJ commented Aug 16, 2018

Numba is very easy to implement, but not quite as fast as Cython...but very easy to implement

import numpy as np
from numba import autojit, jit

@autojit
def inc(arr, i, j):
    arr[i, j] += 1

@autojit
def test5(arr):
    for i in xrange(arr.shape[0]):
        for j in xrange(arr.shape[1]):
            inc(arr, i, j)

As the author on stackoverflow comments:

Even though it's 4.7x slower than Cython, most likely because the JIT compiler failed to inline inc(), I think it is AWESOME! All I needed to do is to add @autojit and didn't have to mess up the code with clumsy type declarations; 88x speedup for next to nothing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Issues that adress a hipothesis or need further insights in order to be resolved
Projects
None yet
Development

No branches or pull requests

2 participants