Package: comptime
comptime"Comptime" accelerates Python code by precomputing complex calculations, turning them into simple lookups for faster
execution.Table of ContentsInstallationUsageLicenseComptime is inspired by the concept of compile-time computation found in languages
likeZig, 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-conceptThis 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:pythonexamples/perf.py# pre-comptime:# Function executed in: 2.0992 seconds total; avg of 209.92 ns. per execution.pythonexamples/perf_match.py# comptime with match-case strategy:# Function executed in: 21.5824 seconds total; avg of 2158.24 ns. per execution.pythonexamples/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!Installationpip install comptimeUsage# src_raw/main.py: before comptimefromcomptimeimportcomptime@comptime.skipdefnonpure_method():# this method has side effects so should only be used at runtimeprint("e.g. sending an email")@comptimedefmy_method():...# some expensive calculationsnonpure_method()# not executed due to @comptime.skipreturn41+1@comptime("users","posts")defcall_api(endpoint):# api get value for endpoint...returnvaluecall_api("other")# possible comptime warning due to unsupported argument?comptime--inputsrc_raw--outputsrc_compiled# src_compiled/main.py: after comptimeimporttypingdefnonpure_method():# this method has side effects so should only be used at runtimeprint("e.g. sending an email")defmy_method():# computed by comptimereturn42defcall_api(endpoint:typing.Literal["users","posts"]):# computed by comptimematchendpoint:case"users":return['user1']case"posts":return['post1']case_:raiseValueError("Uncompiled variant{endpoint}")returnvalueAcknowledgmentsThis 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
byZig, 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.Licensecomptimeis distributed under the terms of theMITlicense.