Skip to main content

"Comptime" accelerates Python code by precomputing complex calculations, turning them into simple lookups for faster execution.

Project description

comptime

PyPI - Version PyPI - Python Version
Code style: black License: MIT
comptime checks Coverage

"Comptime" accelerates Python code by precomputing complex calculations, turning them into simple lookups for faster execution.


Table of Contents

Comptime is inspired by the concept of compile-time computation found in languages like Zig, and it brings this powerful feature into the world of Python, an interpreted language. By utilizing special decorators, you can mark functions whose return values should be precomputed. This enables you to separate computationally expensive parts of your code, performing those calculations once, and embedding the results directly into your source code. The result is Python code that executes faster by turning complex calculations into simple lookups. Whether you're optimizing critical performance bottlenecks or exploring new ways to structure your code, Comptime offers a novel approach to accelerate your Python development.

"Zig into Python's speed lane with Comptime – it's not a sprint; it's a compile!"

:warning: Warning: this is only a proof-of-concept

This code was only created to test the concept. It should NOT be used in a production environment, as I can not guarantee at this time that the semantics of the outputted code are correct. Furthermore, initial performance tests show that for relatively simple calculations, this does NOT actually improve performance:

python examples/perf.py # pre-comptime:
# Function executed in: 2.0992 seconds total; avg of 209.92 ns. per execution.
python examples/perf_match.py # comptime with match-case strategy:
# Function executed in: 21.5824 seconds total; avg of 2158.24 ns. per execution.
python examples/perf_dict.py # comptime with dict lookup strategy:
# Function executed in: 152.4098 seconds total; avg of 15240.98 ns. per execution.

While this package could be useful in cases where the calculation is actually heavy, it could also negatively impact your performance!

Installation

pip install comptime

Usage

# src_raw/main.py: before comptime
from comptime import comptime


@comptime.skip
def nonpure_method():
    # this method has side effects so should only be used at runtime
    print("e.g. sending an email")


@comptime
def my_method():
    ...
    # some expensive calculations
    nonpure_method()  # not executed due to @comptime.skip
    return 41 + 1


@comptime("users", "posts")
def call_api(endpoint):
    # api get value for endpoint
    ...
    return value


call_api("other")  # possible comptime warning due to unsupported argument?
comptime --input src_raw --output src_compiled
# src_compiled/main.py: after comptime
import typing


def nonpure_method():
    # this method has side effects so should only be used at runtime
    print("e.g. sending an email")


def my_method():
    # computed by comptime 
    return 42


def call_api(endpoint: typing.Literal["users", "posts"]):
    # computed by comptime
    match endpoint:
        case "users":
            return ['user1']
        case "posts":
            return ['post1']
        case _:
            raise ValueError("Uncompiled variant {endpoint}")
    return value

Acknowledgments

This project owes its inspiration and certain elements of its design to various sources:

  • Zig Programming Language: The concept of compile-time computation in Comptime is inspired by Zig, a language that emphasizes safety, performance, and readability. A special thank you to Zig's creators and community for their innovative approach to programming.

  • GPT-4 by OpenAI: Assistance with the project boilerplate, creative brainstorming, and crafting the memorable slogan "Zig into Python's speed lane with Comptime – it's not a sprint; it's a compile!" was provided by GPT-4. It also wrote this section.

Please note that the author of comptime is not affiliated with Zig, OpenAI, or any other entities mentioned above. The acknowledgments are expressions of gratitude and inspiration and do not imply any formal association or endorsement by these parties.

License

comptime is distributed under the terms of the MIT license.

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

comptime-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

comptime-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file comptime-0.1.0.tar.gz.

File metadata

  • Download URL: comptime-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for comptime-0.1.0.tar.gz
Algorithm Hash digest
SHA256 de61861cee91de744a2701992c730243ec4523ffc66a214246655fcaac7f8b7c
MD5 5f271c490b1cf5a4acb893850c1fd32a
BLAKE2b-256 85c204dff289fc1b517bdb11326287558bf9ee61a59f9bfa2100abf4491c990e

See more details on using hashes here.

File details

Details for the file comptime-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: comptime-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for comptime-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 397b2c096bca055c90e2b0aaed0d7927285479a38505403ba76b3dfea0eaae73
MD5 37e3f082c2dd28df111425f3383c1550
BLAKE2b-256 eaf78f0a854913d9c599d5f23a72acb6d998d71a9d26ceb65806d75a32c06584

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