Can Mypy speedup python scripts

Hi forum,

  1. Can Mypy run the scripts after they have been checked? I do not know a better way to use Mypy, so I call Mypy and python3 separately to check and run the scripts.

  2. Mypy re-checks scripts which have been checked already. Can makefile be applied to avoid the re-checks?

  3. Is there a way for CPython to know that a script has been checked by Mypy? Can CPython utilize the static typing info provided by Mypy and compile scripts statically and speedup running scripts finally?

  4. In my test, there is no improvement when I use Mypy. It takes same time as CPython running the script.

$ mypy && python3
Success: no issues found in 1 source file


import random
import math

def f(n: int) -> int:
    L: list[float] = []
    for i in range(n):
        x: float = random.random()
        x = math.sqrt(x)
        x = math.pow(x, 2)
        x += x
        x /= 2
    return len(L)


# test:

$ python3 -m pip install mypy
$ mypy appdir/ && date && python3 appdir/ && date
Success: no issues found in 1 source file
Mon Mar 28 01:50:34 CST 2022
Mon Mar 28 01:51:36 CST 2022

# numba:    4 secs
# pypy:    15 secs
# CPython: 60 secs
# Mypy:    60 secs

No, mypy is only a type-checker. You still run your Python program with a Python interpreter.

See Introduction — Mypy 0.942 documentation

No, the Python interpreter cannot know that you have checked a script with mypy, or any other linter or type-checker, or a spell-checker looking for typos in docstrings, or a code reformatter. How could it know?

No, the Python interpreter cannot use the static type checking to speed up scripts. Static typing is for checking program correctness, not for optimizing incorrect code.

If you are interested in speeding up Python, you might be interested in Facebook’s “Cinder” project, as well as projects like Pyston, Cython, and many more.

1 Like

There’s also mypyc, by the same team as mypy, and based on it.

1 Like

Which, to be clear, does basically as @ljh has asked—it uses the static type information checked by MyPy to compile Python code to fast C extensions, which can greatly improve execution speed over just running the Python code directly in the Python interpreter. Also, Cython can use static type hints, as well as other custom C-like syntax to speed up your code.

See here for a comparison; TL;DR, mypyc is specifically optimized to take maximum advantage of mypy type annotations and inference without having to annotate everything or use custom syntax, particularly for general purpose code, while Cython is better suited for heavy numeric/scientific computation, particularly provided you put a bit more effort into it.

1 Like

More information about Cinder and alternatives:


“In May 2020 AI specialists DLabs tested JavaScript versus Python performance for machine learning. For JavaScript Node 12.16.1 was used, and for Python 3.7.6. The results seem surprising: although JavaScript benefits from an excellent JIT in Node (which uses the V8 engine as used by Google Chrome), Python easily outperformed it.”

1 Like

Thanks Jeanas,

The doc does not mention __hash__, __lt__. I can not put class instances into list and dict for sort and search then.

If __hash__ and __lt__ are not listed under “Differences from Python”, doesn’t that mean that hash and lt are the same as in Python? So you can use them.

1 Like