Is Python interpreted or compiled? On Doubling Down and Semantics.
Tags: personal, python, compiler
Published: 2022-06-05 17:13:15

This is a personal blog than a technical one.

It was a lovely Sunday night. Someone on a programming Discord posted an article about a Python to C++ transpiler. I knew this sentiment would come up.

> "Still, Python is way slower because of the massive overhead of being an interpreted language..."

Anyone in the tech space has probably heard and read that Python *is* slow. It is. This picture below should paint a good enough picture of that.


![N-Body Problem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w06lv6gmcaz3subeh3yd.jpg)

In the picture, also look at Node JS. JavaScript isn't compiled, but can toe-to-toe with speeds compared to compiled languages. This picture is a slide from Anthony Shaw's *Restarting Pyjion, a general purpose JIT for Python- is it worth it?*

{% youtube YFeUUdKBrJ8 %}

Anthony Shaw is a Microsoft employee and author of [CPython Internals](https://realpython.com/products/cpython-internals-book/). He has also given another talk [Why is Python Slow?](https://www.youtube.com/watch?v=I4nkgJdVZFA). I would think he's someone who knows what he's talking about.

On both talks, Anthony has given a good introduction to how Python runs code under the hood. **tl;dw**: Your code gets converted through multiple steps all the way down to bytecode, which is then continually evaluated by the Python interpreter.

The compilation step to bytecode, which is done *ahead-of-time (AOT)*, could be made significantly faster if it would have been done *just-in-time (JIT)* instead. In fact, projects such as [PyPy (with JIT)](https://www.pypy.org/) and [Pyjion](https://github.com/tonybaloney/Pyjion) use JIT compilation.

> Python's performance problems come from it being AOT compiled and less from it being interpreted.
> 
> \- me

That's what I said. Verbatim. And it looks like I rattled some nest.

"Compiled?"

"Compiled?"

"Compiled???"

And especially *this* guy.

> Compiled?
>
> Python is compiled?
>
> If Python is compiled, then boy... do I have a bridge for you.

That was indeed, an invitation for engagement. So I replied,

> Language -> AST -> CFG -> bytecode -> ceval.c
>
> .pyc files are basically compiled bytecode
>
> But go ahead and sell me that bridge.

I don't think he has sold me that bridge, especially if his reply consist of him saying that his "personal definition" of compilation refers to compilation to machine-level compilation. Arguing with a stranger's "personal definitions" over the internetz would be a really productive way to spend a Sunday night, wouldn't it? Improving Python's performance was *the* topic I really hoped to have after all.

I would just leave here a snippet from the sidebar of [the Wikipedia article on Ahead-of-Time Compilation](https://en.wikipedia.org/wiki/Ahead-of-time_compilation).

> Notable Runtimes
>
> - Android Runtime (ART)
> - Common Language Runtime (CLR) and Mono
> - crt0
> - Java virtual machine (JVM)
> - Objective-C and Swift
> - V8 and Node.js
> - CPython and PyPy
> - Zend Engine (PHP)
> - LuaJIT
> 

If anything, I believe now more than ever that finding a specialist on a programming language do have value.