Friday, May 1, 2026
banner
Top Selling Multipurpose WP Theme

The opposite day I found an attention-grabbing library that I had by no means heard of earlier than.

PythonC is a domain-specific language (DSL) compiler that enables builders to jot down C applications utilizing commonplace Python syntax. It takes a statically typed subset of Python code and compiles it on to native machine code by way of LLVM IR (Low-Stage Digital Machine Intermediate Illustration).

LLVM IR is A platform-independent code format used internally by the LLVM compiler framework. The compiler first converts the supply code into LLVM IR, after which LLVM converts the IR into machine code optimized for a particular CPU (x86, ARM, and so on.).

The core design philosophy of PythoC is: Runtime equal to C + compile time utilizing Python, And it has nearly distinctive promoting factors comparable to:

1. Create a standalone native executable

Not like instruments comparable to Cython, that are primarily used to create C extensions to hurry up current Python scripts, PythoC can generate utterly impartial, standalone C-style executables. As soon as compiled, the ensuing binary doesn’t require a Python interpreter or rubbish collector to run.

2. Python syntax permits low-level management

PythoC mirrors the options of C, however wraps them in Python’s cleaner syntax. To perform this, we use machine-native kind hints as a substitute of Python’s commonplace dynamic sorts.

  • primitive: i32, i8, f64 and so on.
  • Reminiscence construction: pointer (ptr[T]), array (array[T, N]), and buildings (created by adorning commonplace Python courses).
  • Guide reminiscence administration: Reminiscence administration is specific, just like C, since there isn’t any rubbish collector by default. Nonetheless, it gives trendy elective security checks comparable to: linear kind (ensure that all allocations are explicitly deallocated to forestall leaks) Filter kind (to power compile-time validation checks).

Python as a metaprogramming engine

Certainly one of PythoC’s strongest options is its dealing with of compilation steps. As a result of the compile-time surroundings is simply Python, you need to use commonplace Python logic to generate, manipulate, and specialize PythoC code. in entrance Compiles to LLVM. This provides you very versatile compile-time code technology capabilities (just like C++ templates, however pushed by pure Python).

Sounds promising, however does the fact reside as much as the hype? Now, let’s examine this library in motion. Set up is straightforward, like most Python libraries, simply pip set up:

pip set up pythoc

Nonetheless, it’s higher to arrange a correct growth surroundings that lets you silo totally different tasks. This instance makes use of the UV utility, however be happy to make use of whichever technique is most snug for you. Enter the next command in your command line terminal:

C:Usersthomaprojects> cd tasks
C:Usersthomaprojects> uv init pythoc_test
C:Usersthomaprojects> cd pythoc_test
C:Usersthomaprojectspythoc_test> uv venv --python 3.12
C:Usersthomaprojectspythoc_test> .venvScriptsactivate
(pythoc_test) C:Usersthomaprojectspythoc_test> uv pip set up pythoc

easy instance

To make use of PythoC, outline features with a particular machine kind and mark them for PythoC compilation. Decorator. There are two foremost methods to run PythoC code. You’ll be able to name the compiled library immediately from Python as follows:

from pythoc import compile, i32

@compile
def add(x: i32, y: i32) -> i32:
    return x + y

# Can compile to native code
@compile
def foremost() -> i32:
    return add(10, 20)

# Name the compiled dynamic library from Python immediately
consequence = foremost()
print(consequence)

Then run it like this:

(pythoc_test) C:Usersthomaprojectspythoc_test>python test1.py

30

Alternatively, you possibly can create a standalone executable that may be run independently of Python. To do that, use code like this:

from pythoc import compile, i32

@compile
def add(x: i32, y: i32) -> i32:
    print(x + y)
    return x + y

# Can compile to native code
@compile
def foremost() -> i32:
    return add(10, 20)

if __name__ == "__main__":
    from pythoc import compile_to_executable
    compile_to_executable()

We do it the identical approach.

(pythoc_test) C:Usersthomaprojectspythoc_test>python test4.py

Efficiently compiled to executable: buildtest4.exe
Linked 1 object file(s)

This time, no output is displayed. As an alternative, PythoC creates a construct listing underneath the present listing and creates an executable file there.

(pythoc_test) C:Usersthomaprojectspythoc_test>dir buildtest4*
 Quantity in drive C is Home windows
 Quantity Serial Quantity is EEB4-E9CA

 Listing of C:Usersthomaprojectspythoc_testbuild

26/02/2026  14:32               297 test4.deps
26/02/2026  14:32           168,448 test4.exe
26/02/2026  14:32               633 test4.ll
26/02/2026  14:32               412 test4.o
26/02/2026  14:32                 0 test4.o.lock
26/02/2026  14:32         1,105,920 test4.pdb

You’ll be able to run the test4.exe file identical to every other executable file.

(pythoc_test) C:Usersthomaprojectspythoc_test>buildtest4.exe

(pythoc_test) C:Usersthomaprojectspythoc_test>

However wait a minute. Within the Python code, I explicitly requested for the addition consequence to be printed, however no output is displayed. what occurred?

The reply is that the built-in Python print() perform depends on the Python interpreter working within the background to find out learn how to show the thing. PythoC strips all of this away and builds small, super-fast native executables, so print statements are stripped away.

To print to the display screen in native binary, you need to use commonplace C library features. printf.

How one can use printf in PythonC

C (and due to this fact PythoC) requires a format specifier to print variables. Write a string containing a placeholder (comparable to %d for a decimal integer) and move the variable to be inserted into the placeholder.

This is learn how to replace your code to import the C printf perform and use it accurately.

from pythoc import compile, i32, ptr, i8, extern

# 1. Inform PythoC to hyperlink to the usual C printf perform
@extern
def printf(fmt: ptr[i8], *args) -> i32:
    move

@compile
def add(x: i32, y: i32) -> i32:
  
    printf("Including 10 and 20 = %dn", x+y)
    return x + y

@compile
def foremost() -> i32:
    consequence = add(10, 20)
    
    # 2. Use printf with a C-style format string. 
    # %d is the placeholder for our integer (consequence).
    # n provides a brand new line on the finish.
   
    
    return 0

if __name__ == "__main__":
    from pythoc import compile_to_executable
    compile_to_executable()

Now, in the event you re-run the above code and run the ensuing executable, the output shall be as anticipated.

(pythoc_test) C:Usersthomaprojectspythoc_test>python test5.py
Efficiently compiled to executable: buildtest5.exe
Linked 1 object file(s)

(pythoc_test) C:Usersthomaprojectspythoc_test>buildtest5.exe
Including 10 and 20 = 30

However is it actually well worth the hassle?

Every little thing we have talked about thus far is just of worth in the event you see actual pace enhancements in your code. So, as a last instance, let’s check out how briskly our compiled program is in comparison with its Python equal. This could clearly reply our query.

First, it is common Python code. Use recursive Fibonacci calculations to simulate long-running processes. Let’s calculate the fortieth Fibonacci quantity.

import time

def fib(n):
    # This calculates the sequence recursively
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)

if __name__ == "__main__":
    print("Beginning Normal Python pace take a look at...")
    
    start_time = time.time()
    
    # fib(38) often takes round 10 seconds in Python, 
    # relying in your pc's CPU.
    consequence = fib(40) 
    
    end_time = time.time()
    
    print(f"Consequence: {consequence}")
    print(f"Time taken: {end_time - start_time:.4f} seconds")

I obtained this consequence after I ran the above code.

(pythoc_test) C:Usersthomaprojectspythoc_test>python test6.py
Beginning Normal Python pace take a look at...
Consequence: 102334155
Time taken: 15.1611 seconds

Subsequent, we’ll talk about the PythoC-based code. Once more, as with the print assertion within the earlier instance, you can not simply use common import timing directives from Python for timing. As an alternative, you must borrow commonplace timing features immediately from the C programming language. clock(). That is outlined in the identical approach because the printf assertion used earlier.

That is an up to date PythoC script with a built-in C timer.

from pythoc import compile, i32, ptr, i8, extern

# 1. Import C's printf
@extern
def printf(fmt: ptr[i8], *args) -> i32:
    move

# 2. Import C's clock perform
@extern
def clock() -> i32:
    move

@compile
def fib(n: i32) -> i32:
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)

@compile
def foremost() -> i32:
    printf("Beginning PythoC pace take a look at...n")
    
    # Get the beginning time (this counts in "ticks")
    start_time = clock()
    
    # Run the heavy calculation
    consequence = fib(40)
    
    # Get the tip time
    end_time = clock()
    
    # Calculate the distinction. 
    # Notice: On Home windows, 1 clock tick = 1 millisecond.
    elapsed_ms = end_time - start_time
    
    printf("Consequence: %dn", consequence)
    printf("Time taken: %d millisecondsn", elapsed_ms)
    
    return 0

if __name__ == "__main__":
    from pythoc import compile_to_executable
    compile_to_executable()

My output this time is

(pythoc_test) C:Usersthomaprojectspythoc_test>python test7.py
Efficiently compiled to executable: buildtest7.exe
Linked 1 object file(s)

(pythoc_test) C:Usersthomaprojectspythoc_test>buildtest7.exe
Beginning PythoC pace take a look at...
Consequence: 102334155
Time taken: 308 milliseconds

On this small instance, the code is a little more advanced, however it exhibits the actual advantages of utilizing a compiled language like C. Our executable was 40 instances quicker than the equal Python code. Not too shabby.

Who’s PythonC for?

There are three foremost kinds of PythoC customers.

1/ As we noticed within the Fibonacci pace take a look at, commonplace Python can grow to be gradual when performing heavy mathematical lifting. PythoC may be helpful for Python builders who construct physics simulations, advanced algorithms, or customized knowledge processing pipelines and hit efficiency partitions.

2/ Programmers who work intently with pc {hardware} (constructing recreation engines, writing drivers, programming small IoT gadgets, and so on.) usually write in C as a result of they should manually handle the pc’s reminiscence.

PythoC could also be enticing to those builders as a result of it presents related guide reminiscence management (utilizing pointers and native sorts), however lets you use Python as a “metaprogramming” engine to jot down cleaner and extra versatile code earlier than it’s compiled to the {hardware} stage.

3/ When you write a helpful Python script and wish to share it with a colleague, that colleague usually wants to put in Python, arrange a digital surroundings, and obtain dependencies. This generally is a trouble, particularly in case your goal customers have low IT literacy. Nonetheless, with PythoC, when you create a compiled C executable file, anybody can run it by merely double-clicking the file.

And who’s it not for?

The flip facet of the above is that PythoC might be not one of the best instrument for net builders as a result of the efficiency bottleneck is often the community or database pace, not the CPU calculation pace.

Equally, in the event you’re already utilizing an optimized library comparable to NumPy, you will not see a lot profit.

abstract

This text launched a comparatively new and unknown PythoC library. It lets you write blazingly quick standalone C executable code utilizing Python.

Now we have supplied some examples of making C executable applications utilizing Python and the PythoC library. It additionally consists of examples that present spectacular pace enhancements when working executables created by the PythoC library in comparison with commonplace Python applications.

One drawback you would possibly run into is that Python imports should not supported in PythoC applications, however we additionally confirmed you learn how to work round this drawback by changing them with equal C built-in features.

Lastly, I mentioned my ideas on which kinds of Python programmers would possibly profit from utilizing PythonC of their workloads, and which kinds of Python programmers won’t.

I hope this has impressed you to seek out out what sorts of use instances PythoC can be utilized for. You’ll be able to be taught extra about this convenient library by testing the GitHub repository on the following hyperlink.

https://github.com/1flei/PythoC

banner
Top Selling Multipurpose WP Theme

Converter

Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner
Top Selling Multipurpose WP Theme

Leave a Comment

banner
Top Selling Multipurpose WP Theme

Latest

Best selling

22000,00 $
16000,00 $
6500,00 $
900000,00 $

Top rated

6500,00 $
22000,00 $
900000,00 $

Products

Knowledge Unleashed
Knowledge Unleashed

Welcome to Ivugangingo!

At Ivugangingo, we're passionate about delivering insightful content that empowers and informs our readers across a spectrum of crucial topics. Whether you're delving into the world of insurance, navigating the complexities of cryptocurrency, or seeking wellness tips in health and fitness, we've got you covered.