Skip to main content

Speed up Python code that has well layed out type hints (works by converting the function to typed cython). Find more info at https://github.com/smpurkis/autocompile

Project description

AutoCompile

TLDR; Speed up Python code that is marked with type hints (by converting it to Cython)

This is a package born slightly out of surprise when I found out that type hints don't speed up Python code at all, when all the information is there to be able to speed it up. So I decided to write this short package, that analyzes the code of any function marked with @autocompile and converts it into a Cython inline function. For example,

def do_maths(x: float):
    i: int
    for i in range(10000000):
        x += (i + x) ** 0.1
    return x

will be converted to:

def do_maths(double x):
    cdef long i  
    for i in range(10000000):
        x += (i + x) ** 0.1
    return x

Documentation

@autocompile has the following arguments:

    mode: "inline" or "file", type: str, default: "inline"
        "inline": uses Cython inline as a backend, works with all imported libraries
        "file": moves code to a tmp file and cythonizes it using subprocess, doesn't work with any imported libraries
    infer_types: True or False, type: Bool, default: False
        Enable Cython infer type option
    checks_on: True or False, type: Bool, default: False
        Enable Cython boundary and wrapping checking
    required_imports: {} or globals(), type: Dict, default: {}
        This is required for access to the globals of the calling module. As Python in its infinite wisdom doesn't allow
        access without explicitly passing them.
        Example:
            @autocompile(required_imports=globals())
            def foo(bar: int):
                x = np.arange(bar)
                return x
        Without passing globals, Cython inline conversion will error, as it doesn't know what np (numpy) is.
    debug: True or False, type: Bool, default: False
        Shows the created function code to be used in place of the original
    force_memview: True or False, type: Bool, default: False (currently disabled)
        Forces all declared numpy arrays to be treated at cython memview. Can be unsafe, as addition of memviews
        in cython is not supported while for numpy arrays it is.

Benchmark

Here are a few benchmarks of speed improvements (all code is in tests folder):

tests/test_main.py::test_mixed_maths 
maths_py took: 1.263 seconds
maths_nb took: 0.498 seconds
func_cy took: 2.489 seconds
maths_ac took: 0.486 seconds
PASSED

tests/test_main.py::test_list_type 
lists_py took: 0.091 seconds
lists_nb took: 0.042 seconds
func_cy took: 0.049 seconds
lists_ac took: 0.053 seconds
PASSED

tests/test_main.py::test_mixed_types 
mixed_py took: 0.513 seconds
mixed_nb took: 0.292 seconds
func_cy took: 0.199 seconds
mixed_ac took: 0.064 seconds
PASSED

tests/test_main.py::test_np_arr_in_body 
np_array_in_body_py took: 0.494 seconds
np_array_in_body_nb took: 0.017 seconds
func_cy took: 0.45 seconds
np_array_in_body_ac took: 0.467 seconds
PASSED

tests/test_main.py::test_np_arr_in_args 
np_array_in_args_py took: 0.443 seconds
np_array_in_args_nb took: 0.011 seconds
np_array_in_args_np took: 0.01 seconds
np_array_in_args_ac took: 0.016 seconds
PASSED

tests/test_main.py::test_strings 
string_py took: 0.049 seconds
string_nb took: 0.795 seconds
func_cy took: 0.721 seconds
string_ac took: 0.038 seconds
PASSED

notes:

  • The test_strings is using cython version 3.0a6, using <3.0 yields results similar to numba.
  • This is using cython.compile, to compare against, as it is the closest function to autocompile (ac).

As can be seen, ac is best at a mixture of base Python types, lists, dicts, numbers. It offers selective speed up for arrays at the moment (via numpy array inputs as arguments)

Potential improvements:

  • Add support for return types (relatively straightforward)
  • Add a backend like Nim or Julia (a lot of work)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autocompile-0.3.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distributions

autocompile-0.3.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

autocompile-0.3.0-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file autocompile-0.3.0.tar.gz.

File metadata

  • Download URL: autocompile-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for autocompile-0.3.0.tar.gz
Algorithm Hash digest
SHA256 50b5d0742cba53932a2bc61b1339b6eed79c1b9dc37045cf0209eae9efd688f2
MD5 f052cd1a12dc278e8a327b9ccc25c9b3
BLAKE2b-256 06c11d8f8eb8423d51bb7635a901df779a92e51ecafc074b57b77736dd158153

See more details on using hashes here.

File details

Details for the file autocompile-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: autocompile-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for autocompile-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7211afebcf072f5afb8b0e1267ceca85b72bda921b9a9db718349ceaf77132ba
MD5 b91b95b1b740958326368ff1ce4c9c74
BLAKE2b-256 9491c9e3b98fe5ceaf0523e07f164c8c961012b05f33b3925e0d1680bd293c34

See more details on using hashes here.

File details

Details for the file autocompile-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: autocompile-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for autocompile-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2478bf74ea3b2dd8b9a5aa311c2240c101ad01de875a4f189c8aed0d4cb6602e
MD5 f19ef1daa49abd6a3bac7078c4d32db0
BLAKE2b-256 d54f11da4e112880caab366af068499c1aa2b77479918bcdcac39cf1d3373ad3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page