2023-02-23

mypyc is quick

mypyc doesn't get a huge amount of attention, but it's easy to use and can make well-typed Python really fast. No one likes benchmarks, but here's one anyways.

Save the following as lib.py

from dataclasses import dataclass

@dataclass
class Row:
    name: str
    age: int

def make() -> list[Row]:
    return [Row(name=f"name-{i}", age=i) for i in range(1_000_000)]

def mult(rows: list[Row]) -> None:
    for row in rows:
        row.name = row.name.upper()
        row.age *= 3

And the following as prof.py

import time
import lib

before  = time.time()
d = lib.make()
print(f"took {time.time() - before:.2f} seconds")

before  = time.time()
lib.mult(d)
print(f"took {time.time() - before:.2f} seconds")

Now python prof.py

On my laptop, make() runs in 1.95s, mult() runs in 0.87s.


Now compile it, pip install mypy , mypyc lib.py

On my laptop, make() runs in 1.40s, mult() runs in 0.03s.


Quick huh!

This could be quite an exciting development in the Python ecosystem. Imagine writing interpreted code all day (zero compile times, breakpoints, etc), then compiling for production. Imagine a well-typed and compiled stdlib, flask, SQLAlchemy, etc. It's gonna be great ☀️.